Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#2261 closed enhancement (wontfix)

[patch] Make model instances implement __hash__

Reported by: phil.h.smith@… Owned by: adrian
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: UI/UX:


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 10 years ago.
patch to add a hash impl to Model

Download all attachments as: .zip

Change History (6)

Changed 10 years ago by anonymous

patch to add a hash impl to Model

comment:1 Changed 10 years ago by anonymous

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

comment:2 Changed 10 years ago by anonymous

  • Summary changed from Make model instances implement __hash__ to [patch] Make model instances implement __hash__

comment:3 Changed 10 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 10 years ago by jacob

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:5 Changed 8 years ago by aconbere

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