Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#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 (none), 16 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:2 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 by Malcolm Tredinnick, 16 years ago

Component: Database layer (models, ORM)File uploads/storage

comment:4 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [9997]) Fixed #9508 -- Added an appropriate FileField.hash implementation.

Required because we declare a custom eq method.

comment:5 by Malcolm Tredinnick, 16 years ago

(In [10000]) [1.0.x] Fixed #9508 -- Added an appropriate FileField.hash implementation.

Required because we declare a custom eq method.

Backport of r9997 from trunk.

comment:6 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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