Code

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#9786 closed (fixed)

Files (and subclasses) override __eq__ but not __ne__

Reported by: xtian <xtian@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

We were trying to compare two model objects using the following function:

def allModelFieldsEqual(a, b):
    fields = [f.name for f in a._meta.fields]
    for name in fields:
        if getattr(a, name) != getattr(b, name):
            return False
    return True

This fails when the models contain FileFields, because File doesn't implement ne. Confusingly, because it implements eq, the values also compare equal.

(Pdb) !a.largeImage
<ImageFieldFile: /images/first-large-image.png>
(Pdb) !b.largeImage
<ImageFieldFile: /images/first-large-image.png>
(Pdb) !a.largeImage == b.largeImage
True
(Pdb) !a.largeImage != b.largeImage
True

It should probably be implemented as:

    def __ne__(self, other):
        return not self == other

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by mtredinnick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed

Fixed in r9647.

comment:2 Changed 5 years ago by mtredinnick

(In [9648]) [1.0.X] Fixed #9786 -- Fixed inequality checking for django.db.models.fields.file.FieldFile class.

Backport of r9647 from trunk.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.