UUID as default value in Django model

后端 未结 2 1779
小鲜肉
小鲜肉 2021-02-07 07:00

I\'ve noticed the strange behaviour of default value in django model. For example we have a simple django model:

import uuid
...

class SiteUser(models.Model):
          


        
2条回答
  •  忘了有多久
    2021-02-07 07:36

    Problem is the default attribute that you are setting as

    activation_key = models.CharField(max_length=64, verbose_name=u"Activation key",
                     default=uuid.uuid1())
    

    Here you are setting the default value as not a callable but value returned by uuid.uuid1() call when this model class is initialized.

    You should set it as default=uuid.uuid1 which sets it as callable, and is sets new uuid each time new default value needs to be used.

提交回复
热议问题