class UserInfo(models.Model):
userId = models.IntegerField(primary_key=True)
userName = models.CharField(max_length=30)
score = models.IntegerField(default=0)
countScore = models.IntegerField(default=0)
有这样一个对象,然后
ui = UserInfo(userName='user'+str(random.randint(1,10000)))
print ui.userId
ui.save()
print ui.userId
按照官网文档:
https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#auto-incrementing-primary-keys
应该第二次调用ui.userId 会得到返回值,但是,却没有。
非常奇怪
解决办法:
将IntegerField 换成 AutoField
解决:http://stackoverflow.com/questions/7689553/empty-id-after-saving-model-to-database