John Doe
John Doe

Reputation: 1639

Django Model as Python dictionary key

I need to build a dictionary which would have Django Models as keys but I get this error on a default made model:

TypeError: Model instances without primary key value are unhashable

yet, Django documentation says that it takes care of building id as default primary key.

So why do I get this error?

Upvotes: 0

Views: 640

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

Note that the error message explicitely mentions an empty pk value. IOW: you have to save the model instance first, serial (auto) id fields only get their values when the record is inserted in the table.

Upvotes: 4

Related Questions