#2261 closed enhancement (wontfix)
[patch] Make model instances implement __hash__
| Reported by: | 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)
Change History (6)
by , 19 years ago
| Attachment: | base.py.patch added |
|---|
comment:1 by , 19 years ago
err, please read the underlined 'eq' and 'hash' as having the double underscore on either side.
comment:2 by , 19 years ago
| Summary: | Make model instances implement __hash__ → [patch] Make model instances implement __hash__ |
|---|
comment:3 by , 19 years ago
| Cc: | 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 , 19 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
Models shouldn't have a __hash__ method because models are mutable, and hence can't be used for keys. Marking wontfix.
comment:5 by , 18 years ago
models might be mutable, but aren't model instances just as immutable as any variable type?
patch to add a hash impl to Model