Opened 6 weeks ago

Last modified 6 weeks ago

#31750 assigned Bug

Abstract model field should not be equal across models

Reported by: Ryan Hiebert Owned by: Ryan Hiebert
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Ryan Hiebert Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Consider the following models:

class A(models.Model):
    class Meta:
        abstract = True
    myfield = IntegerField()

class B(A):

class C(A):

If I pull the fields of B and C into a shared set, one will be de-duplicated away, because they compare as equal. I found this surprising, though in practice using a list was sufficient for my need. The root of the issue is that they compare equal, as fields only consider self.creation_counter when comparing for equality.

len({B._meta.get_field('myfield'), C._meta.get_field('myfield')}) == 1
B._meta.get_field('myfield') == C._meta.get_field('myfield')

We should adjust __eq__ so that if the field.model is different, they will compare unequal. Similarly, it is probably wise to adjust __hash__ and __lt__ to match.

When adjusting __lt__, it may be wise to order first by self.creation_counter so that cases not affected by this equality collision won't be re-ordered. In my experimental branch, there was one test that broke if I ordered them by model first.

I brought this up on IRC django-dev to check my intuitions, and those conversing with me there seemed to agree that the current behavior is not intuitive.

Change History (2)

comment:1 Changed 6 weeks ago by Carlton Gibson

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

OK, happy to look at a patch — assuming nothing breaks...

It'd be a change in behaviour, so would need calling out in the release notes.

comment:2 Changed 6 weeks ago by Ryan Hiebert

Cc: Ryan Hiebert added
Has patch: set
Owner: changed from nobody to Ryan Hiebert
Status: newassigned


Should this get backported? It feels like an (admittedly esoteric) bug fix to me. It affects ordering, though I've minimized that effect to only places where the ordering is currently undefined, as they are currently considered equal.

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