Opened 13 years ago

Closed 12 years ago

Last modified 11 years ago

#2261 closed enhancement (wontfix)

[patch] Make model instances implement __hash__

Reported by: phil.h.smith@… Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: mir@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Model instances support eq, which compares types and primary keys. They don't define a hash, however. This leads to some fun when making sets of model instances (or using them as map keys.)

One solution is to define hash in django.db.models.base.Model. Please find a patch attached.

Attachments (1) (418 bytes) - added by anonymous 13 years ago.
patch to add a hash impl to Model

Download all attachments as: .zip

Change History (6)

Changed 13 years ago by anonymous

Attachment: added

patch to add a hash impl to Model

comment:1 Changed 13 years ago by anonymous

err, please read the underlined 'eq' and 'hash' as having the double underscore on either side.

comment:2 Changed 13 years ago by anonymous

Summary: Make model instances implement __hash__[patch] Make model instances implement __hash__

comment:3 Changed 13 years ago by mir@…

Cc: mir@… added

Aren't dictionaries and sets only for immutable objects? On the other side, this is very convenient and works as long as you don't change the primary key.

comment:4 Changed 12 years ago by Jacob

Resolution: wontfix
Status: newclosed

Models shouldn't have a __hash__ method because models are mutable, and hence can't be used for keys. Marking wontfix.

comment:5 Changed 11 years ago by Anders Conbere

models might be mutable, but aren't model instances just as immutable as any variable type?

Note: See TracTickets for help on using tickets.
Back to Top