Luis Fernando
Luis Fernando

Reputation: 25

empty ID after saving model to database

I have an "Images" model that has

 imageID = models.IntegerField(primary_key=True, editable=False)

Then I'm doing:

newimage = Images(status=1, createDateTime=datetime.now(), User=user)
newimage.save()

Then I'm trying to retrieve the imageID using newimage.imageID, but it's empty. Even after the row was added to the database.

Any ideas?

Upvotes: 0

Views: 313

Answers (2)

If you are using MSSQL Server, you can set the default valut of the primary key to NEWSEQUENTIALID() and the data type to UNIQUEIDENTIFIER.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599490

You've used an IntegerField, rather than an AutoField, which implies that you're responsible for setting the value yourself rather than letting the database do it. Do you have any such code? If not, why have you used that field? Why not just use the automatic id field?

Upvotes: 3

Related Questions