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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With