我想给模型方法的模型字段默认值。从模型方法的Django模型字段默认
我该怎么做?
当我尝试此代码
Class Person(models.Model):
def create_id(self):
return os.urandom(12).encode('hex')
name = models.CharField(max_length = 255)
id = models.CharField(max_length = 255,default = self.create_id)
我得到NameError:名字 '自我' 是没有定义。
如果我删除'自己'我得到'create_id'需要1个参数。
这个ID是否必须是唯一的? person_id = models.AutoField(primary_key = True)就像一个想法。 – Don 2013-06-14 20:57:01
是的,它必须是 – yossi 2013-06-16 06:51:48