#9508 closed (fixed)
FieldFile should define __hash__
Reported by: | Graham King | Owned by: | nobody |
---|---|---|---|
Component: | File uploads/storage | Version: | 1.0 |
Severity: | Keywords: | FieldFile hash | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
FieldFile defines __eq__ to compare on the name field, but does not define __hash__, hence two FieldFile will be equal but have a different hash. Example:
from django.db.models.fields.files import FileField, FieldFile f = FileField(upload_to='dir') ff1 = FieldFile(None, f, 'dir/file.ext') ff2 = FieldFile(None, f, 'dir/file.ext') print 'Equal?', ff1 == ff2 print 'Hashes are equal?', hash(ff1) == hash(ff2)
I suggest adding the following to FieldFile:
def __hash__(self): return hash(self.name)
I haven't attached a patch because the change is only two lines - I'm happy too if requested.
I noticed this because I'm comparing objects in different databases by comparing the hash of each of their fields.
Change History (6)
comment:1 by , 16 years ago
milestone: | post-1.0 |
---|
comment:2 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 16 years ago
Component: | Database layer (models, ORM) → File uploads/storage |
---|
comment:4 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:5 by , 16 years ago
Note:
See TracTickets
for help on using tickets.
Milestone post-1.0 deleted