Opened 6 years ago

Closed 6 years ago

Last modified 4 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 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:2 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by mtredinnick

  • Component changed from Database layer (models, ORM) to File uploads/storage

comment:4 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

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

Required because we declare a custom eq method.

comment:5 Changed 6 years ago by mtredinnick

(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 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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