Hello,

I want to set the "default" value as a randomly generated String for
the promotion_code part of my Promotion model, for that the
code_generate function is used.

The issue with the code below that it seems like
default=code_generate() generates this random string once every server
start thus assigning the same value. I can see that by the admin
panel, every time I try to generate a new Promotion, it gives me the
exact same string.

Here is the code:

#generate a string, which is not already existing in the earlier
Promotion instances
def code_generate():
    while 1:
        from django.conf import settings
        import random, string
        prom_code = ''.join(random.choice(string.ascii_uppercase +
string.digits) for x in range(6))
        try:
            Promotion.objects.get(promotion_code=prom_code)
        except:
            return prom_code

class Promotion(models.Model):
    purchase = models.ForeignKey('Purchase')
    promotion_code =
models.CharField(max_length=20,unique=True,default=code_generate())

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to