Opened 17 years ago

Closed 17 years ago

#6500 closed (fixed)

Make models hashable

Reported by: Bastian Kleineidam <calvin@…> 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)

0055-Make-models-hashable.patch (853 bytes ) - added by Bastian Kleineidam <calvin@…> 17 years ago.

Download all attachments as: .zip

Change History (7)

by Bastian Kleineidam <calvin@…>, 17 years ago

comment:1 by pytechd <pytechd@…>, 17 years ago

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) and Author(pk=1) are identical.

comment:2 by Bastian Kleineidam <calvin@…>, 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 Simon Greenhill <dev@…>, 17 years ago

Patch needs improvement: set
Triage Stage: UnreviewedReady for checkin

in reply to:  2 comment:4 by Eric, 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 Malcolm Tredinnick, 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 Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(In [7132]) Fixed #2936, #6500 -- Added a hash() method to Models (since we implement our own eq method).

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