Opened 18 years ago

Closed 17 years ago

Last modified 16 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

Description

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)

base.py.patch (418 bytes ) - added by anonymous 18 years ago.
patch to add a hash impl to Model

Download all attachments as: .zip

Change History (6)

by anonymous, 18 years ago

Attachment: base.py.patch added

patch to add a hash impl to Model

comment:1 by anonymous, 18 years ago

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

comment:2 by anonymous, 18 years ago

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

comment:3 by mir@…, 18 years ago

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 by Jacob, 17 years ago

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 by Anders Conbere, 16 years ago

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