Opened 8 years ago

Closed 8 years ago

Last modified 5 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: UI/UX:

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 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:2 Changed 8 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 Changed 8 years ago by Malcolm Tredinnick

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

comment:4 Changed 8 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

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

Required because we declare a custom eq method.

comment:5 Changed 8 years ago by Malcolm Tredinnick

(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 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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