I have noticed strange default behavior in the django model. For example, we have a simple django model:
import uuid ... class SiteUser(models.Model): ... username = models.CharField(max_length=255, verbose_name=u"Username") activation_key = models.CharField(max_length=64, verbose_name=u"Activation key", default=uuid.uuid1())
When I create a new user, and after that another one like this:
user_a = SiteUser(username="UserA") user_a.save() user_b = SiteUser(username="UserB") user_b.save()
Django makes 2 users with the same activation_key
But then I do it like this:
user_a = SiteUser(username="UserA") user_a.activation_key = uuid.uuid1() user_a.save() user_b = SiteUser(username="UserB") user_b.activation_key = uuid.uuid1() user_b.save()
Everything works fine, and Django creates 2 users with different activation keys.
What's going on here? Python loads the model object and calculates the default value of the model when the wsgi application starts, or what? Why does uuid give the same values ββin the first case, but different in the second?
Thanks.
source share