Django - 最后插入的ID [英] Django - last insert id
问题描述
我认为:
comment =评论(...)
comment.save()
comment.id #returns无
在我的模型中:
class Comments(models.Model):
id = models.IntegerField(primary_key = True)
<有人有人遇到这个问题吗?通常在调用save()方法后,我可以通过comment.id访问该id,但是这次不行。
您是否在 comment = Comments(...)
$ c $中设置 id
字段的值c>行?如果没有,你为什么要定义字段,而不是让Django使用AutoField来处理主键?
如果在IntegerField中指定为主键,那么在示例中,Django将不会自动为其赋值一个值。
I can't get the last insert id like I usually do and I'm not sure why.
In my view:
comment = Comments( ...)
comment.save()
comment.id #returns None
In my Model:
class Comments(models.Model):
id = models.IntegerField(primary_key=True)
Has anyone run into this problem before? Usually after I call the save() method, I have access to the id via comment.id, but this time it's not working.
Are you setting the value of the id
field in the comment = Comments( ...)
line? If not, why are you defining the field instead of just letting Django take care of the primary key with an AutoField?
If you specify in IntegerField as a primary key as you're doing in the example Django won't automatically assign it a value.
这篇关于Django - 最后插入的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!