Code

Opened 2 years ago

Closed 2 years ago

Last modified 17 months ago

#17466 closed Bug (duplicate)

Model __eq__ does not compare correctly between normal objects and deferred objects

Reported by: Raphaël Hertzog <hertzog@…> Owned by: nobody
Component: Uncategorized Version: 1.3
Severity: Normal Keywords:
Cc: Enrico, Zini, <enrico@…>, marti@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Enrico Zini <enrico@…> found a problem and reported it to the Debian Bug Tracking System: http://bugs.debian.org/653177

I'll paste his report below:


I noticed that there is a problem with the default model object __eq__
implementation when working with deferred objects.

The default implementation should be something like:

        def __eq__(self, other):
            return isinstance(other, self.__class__) and self._get_pk_val() == other._get_pk_
val()

Now, given a simple model...

        class Package(models.Model):
                name = models.TextField(null=False, unique=True)
                # Lots of other fields

...this code will show the problem:

        # Get a normal object
        p = Package.objects.get(name="debtags")
        # Get a deferred object
        dp = Package.objects.filter(name="debtags").only("name")[0]
        print object.__repr__(p), hash(p)
        print object.__repr__(dp), hash(dp)
        # This assert succeeds
        assert hash(p) == hash(dp)
        # This assert fails
        assert p == dp

Its output is:

        <Package: debtags> 11
        <Package_Deferred_ldesc_popcon_sdesc_source_version: debtags> 11

It looks like the isinstance bit of the default __eq__ model is failing
here.

This causes suprising behaviour:

        allpkgs = set(Package.objects.all().only("name"))
        # This assert fails
        assert Package.objects.get(name="debtags") in allpkgs

Attachments (0)

Change History (2)

comment:1 Changed 2 years ago by kmtracey

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

This looks to be the same problem as described in #11892.

comment:2 Changed 17 months ago by intgr

  • Cc marti@… added

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.