`
2014马年
  • 浏览: 118867 次
  • 性别: Icon_minigender_1
  • 来自: 晋中
社区版块
存档分类
最新评论

django 的自增长id,无法返回插入后的id

阅读更多

 

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

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics