Create a random string each time a model object is saved to database











up vote
0
down vote

favorite












In my models.py, I have:



def MakeOTP():
import random,string
return ''.join(random.choices(string.digits, k=4))


class Prescriptionshare(models.Model):
prid = models.AutoField(primary_key=True, unique=True)
customer = models.ForeignKey(
customer, on_delete=models.CASCADE, null=True)
time = models.DateTimeField(default=timezone.now)
checkin =models.ForeignKey(Checkins, on_delete=models.CASCADE, null=True)
otp = models.CharField(max_length=5, default=MakeOTP())


In my django shell, I've tried the following:



pq = Prescriptionshare(customer = cus, checkin = chk)
pq.save()


The problem is that each time this is executed, I get the same string in otp field. There is no random change of string.
Why is this happening?










share|improve this question






















  • Assuming the OTP is used for security, you must not use the random module. The Mersenne Twister algorithm is completely insecure for cryptographic purposes, with a relatively small sample of outputs, anyone can recover the internal state and predict future outputs. Use the secrets module or django.utils.crypto.get_random_string() instead.
    – knbk
    Nov 7 at 13:18















up vote
0
down vote

favorite












In my models.py, I have:



def MakeOTP():
import random,string
return ''.join(random.choices(string.digits, k=4))


class Prescriptionshare(models.Model):
prid = models.AutoField(primary_key=True, unique=True)
customer = models.ForeignKey(
customer, on_delete=models.CASCADE, null=True)
time = models.DateTimeField(default=timezone.now)
checkin =models.ForeignKey(Checkins, on_delete=models.CASCADE, null=True)
otp = models.CharField(max_length=5, default=MakeOTP())


In my django shell, I've tried the following:



pq = Prescriptionshare(customer = cus, checkin = chk)
pq.save()


The problem is that each time this is executed, I get the same string in otp field. There is no random change of string.
Why is this happening?










share|improve this question






















  • Assuming the OTP is used for security, you must not use the random module. The Mersenne Twister algorithm is completely insecure for cryptographic purposes, with a relatively small sample of outputs, anyone can recover the internal state and predict future outputs. Use the secrets module or django.utils.crypto.get_random_string() instead.
    – knbk
    Nov 7 at 13:18













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In my models.py, I have:



def MakeOTP():
import random,string
return ''.join(random.choices(string.digits, k=4))


class Prescriptionshare(models.Model):
prid = models.AutoField(primary_key=True, unique=True)
customer = models.ForeignKey(
customer, on_delete=models.CASCADE, null=True)
time = models.DateTimeField(default=timezone.now)
checkin =models.ForeignKey(Checkins, on_delete=models.CASCADE, null=True)
otp = models.CharField(max_length=5, default=MakeOTP())


In my django shell, I've tried the following:



pq = Prescriptionshare(customer = cus, checkin = chk)
pq.save()


The problem is that each time this is executed, I get the same string in otp field. There is no random change of string.
Why is this happening?










share|improve this question













In my models.py, I have:



def MakeOTP():
import random,string
return ''.join(random.choices(string.digits, k=4))


class Prescriptionshare(models.Model):
prid = models.AutoField(primary_key=True, unique=True)
customer = models.ForeignKey(
customer, on_delete=models.CASCADE, null=True)
time = models.DateTimeField(default=timezone.now)
checkin =models.ForeignKey(Checkins, on_delete=models.CASCADE, null=True)
otp = models.CharField(max_length=5, default=MakeOTP())


In my django shell, I've tried the following:



pq = Prescriptionshare(customer = cus, checkin = chk)
pq.save()


The problem is that each time this is executed, I get the same string in otp field. There is no random change of string.
Why is this happening?







django django-models






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 10:58









Joel G Mathew

1,84992643




1,84992643












  • Assuming the OTP is used for security, you must not use the random module. The Mersenne Twister algorithm is completely insecure for cryptographic purposes, with a relatively small sample of outputs, anyone can recover the internal state and predict future outputs. Use the secrets module or django.utils.crypto.get_random_string() instead.
    – knbk
    Nov 7 at 13:18


















  • Assuming the OTP is used for security, you must not use the random module. The Mersenne Twister algorithm is completely insecure for cryptographic purposes, with a relatively small sample of outputs, anyone can recover the internal state and predict future outputs. Use the secrets module or django.utils.crypto.get_random_string() instead.
    – knbk
    Nov 7 at 13:18
















Assuming the OTP is used for security, you must not use the random module. The Mersenne Twister algorithm is completely insecure for cryptographic purposes, with a relatively small sample of outputs, anyone can recover the internal state and predict future outputs. Use the secrets module or django.utils.crypto.get_random_string() instead.
– knbk
Nov 7 at 13:18




Assuming the OTP is used for security, you must not use the random module. The Mersenne Twister algorithm is completely insecure for cryptographic purposes, with a relatively small sample of outputs, anyone can recover the internal state and predict future outputs. Use the secrets module or django.utils.crypto.get_random_string() instead.
– knbk
Nov 7 at 13:18












1 Answer
1






active

oldest

votes

















up vote
5
down vote



accepted










remove the () from default=MakeOTP()



class Prescriptionshare(models.Model):
# your code
otp = models.CharField(max_length=5, default=MakeOTP) # here, remove the "()"


After making changes in the models, you should migrate the DB





Why this happening?

If you use MakeOTP(), Django takes the output of the function, where as if you use MakeOTP (without parenthesis) Django consider it as callable function.


That is, when paranthesis used, method is called once the migrations are run and its value is used as the default value and when paranthesis are not used, function reference called everytime while object creation.






share|improve this answer



















  • 1




    To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
    – Ozgur Akcali
    Nov 7 at 11:04










  • @OzgurAkcali Thanks for your tip :)
    – JPG
    Nov 7 at 11:09











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188138%2fcreate-a-random-string-each-time-a-model-object-is-saved-to-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










remove the () from default=MakeOTP()



class Prescriptionshare(models.Model):
# your code
otp = models.CharField(max_length=5, default=MakeOTP) # here, remove the "()"


After making changes in the models, you should migrate the DB





Why this happening?

If you use MakeOTP(), Django takes the output of the function, where as if you use MakeOTP (without parenthesis) Django consider it as callable function.


That is, when paranthesis used, method is called once the migrations are run and its value is used as the default value and when paranthesis are not used, function reference called everytime while object creation.






share|improve this answer



















  • 1




    To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
    – Ozgur Akcali
    Nov 7 at 11:04










  • @OzgurAkcali Thanks for your tip :)
    – JPG
    Nov 7 at 11:09















up vote
5
down vote



accepted










remove the () from default=MakeOTP()



class Prescriptionshare(models.Model):
# your code
otp = models.CharField(max_length=5, default=MakeOTP) # here, remove the "()"


After making changes in the models, you should migrate the DB





Why this happening?

If you use MakeOTP(), Django takes the output of the function, where as if you use MakeOTP (without parenthesis) Django consider it as callable function.


That is, when paranthesis used, method is called once the migrations are run and its value is used as the default value and when paranthesis are not used, function reference called everytime while object creation.






share|improve this answer



















  • 1




    To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
    – Ozgur Akcali
    Nov 7 at 11:04










  • @OzgurAkcali Thanks for your tip :)
    – JPG
    Nov 7 at 11:09













up vote
5
down vote



accepted







up vote
5
down vote



accepted






remove the () from default=MakeOTP()



class Prescriptionshare(models.Model):
# your code
otp = models.CharField(max_length=5, default=MakeOTP) # here, remove the "()"


After making changes in the models, you should migrate the DB





Why this happening?

If you use MakeOTP(), Django takes the output of the function, where as if you use MakeOTP (without parenthesis) Django consider it as callable function.


That is, when paranthesis used, method is called once the migrations are run and its value is used as the default value and when paranthesis are not used, function reference called everytime while object creation.






share|improve this answer














remove the () from default=MakeOTP()



class Prescriptionshare(models.Model):
# your code
otp = models.CharField(max_length=5, default=MakeOTP) # here, remove the "()"


After making changes in the models, you should migrate the DB





Why this happening?

If you use MakeOTP(), Django takes the output of the function, where as if you use MakeOTP (without parenthesis) Django consider it as callable function.


That is, when paranthesis used, method is called once the migrations are run and its value is used as the default value and when paranthesis are not used, function reference called everytime while object creation.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 7 at 11:08

























answered Nov 7 at 10:59









JPG

11.9k2829




11.9k2829








  • 1




    To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
    – Ozgur Akcali
    Nov 7 at 11:04










  • @OzgurAkcali Thanks for your tip :)
    – JPG
    Nov 7 at 11:09














  • 1




    To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
    – Ozgur Akcali
    Nov 7 at 11:04










  • @OzgurAkcali Thanks for your tip :)
    – JPG
    Nov 7 at 11:09








1




1




To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
– Ozgur Akcali
Nov 7 at 11:04




To clarify the situation here, when paranthesis used, method is called once the migrations are run and its value is used as the default value, you need to pass it as a function reference for it to be called everytime
– Ozgur Akcali
Nov 7 at 11:04












@OzgurAkcali Thanks for your tip :)
– JPG
Nov 7 at 11:09




@OzgurAkcali Thanks for your tip :)
– JPG
Nov 7 at 11:09


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188138%2fcreate-a-random-string-each-time-a-model-object-is-saved-to-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()