Opened 17 years ago
Closed 17 years ago
#6500 closed (fixed)
Make models hashable
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Uncategorized | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
The Model instances should be hashable so that tests like
"instance in set([instance1, instance2])" actually succeed.
Attachments (1)
Change History (7)
by , 17 years ago
Attachment: | 0055-Make-models-hashable.patch added |
---|
comment:1 by , 17 years ago
follow-up: 4 comment:2 by , 17 years ago
I don't think hash collisions for different models have a big performance penalty (if any). You usually end up having the same type of objects in a set/list (populated from MyModel.objects.all() or similar). The pk gives a good hash data in this case.
comment:3 by , 17 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Ready for checkin |
comment:4 by , 17 years ago
Replying to Bastian Kleineidam <calvin@debian.org>:
I don't think hash collisions for different models have a big performance penalty (if any). You usually end up having the same type of objects in a set/list (populated from MyModel.objects.all() or similar). The pk gives a good hash data in this case.
That's not entirely true when dealing with the content types framework.
comment:5 by , 17 years ago
I implemented a hash function independent of having seen this ticket and arrived at basically the same logic Bastian's using. The common case will be objects of the same type, using the pk as a hash is very fast (quite a bit faster than hashing a tuple of class and pk) and satisfies Python's requirements, and even if you do get the odd collision in the case of mixed content types, it doesn't matter that much. Simplicity wins here.
comment:6 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Before adding this, please consider adding the app and model name into the hash as well as the PK. Otherwise the hashes of
Article(pk=1)
andAuthor(pk=1)
are identical.