Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#28175 closed Bug (fixed)

__in query on parent model no longer works in 1.11

Reported by: Daniel Keller Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Release blocker Keywords:
Cc: felixxm, Simon Charette Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Daniel Keller)

I have models

class Place(models.Model):
    pass

class Community(Place):
    place_ptr = models.OneToOneField(Place, parent_link=True, on_delete=models.CASCADE,
        primary_key=True, serialize=False, related_name='+')

class Potential(models.Model):
    community = models.ForeignKey(Community, on_delete=CASCADE)

and in 1.10 I can do

Potential.objects.filter(community__in=Place.objects.all())

but in 1.11 this fails with

django.core.exceptions.FieldError: Cannot resolve keyword 'place_ptr' into field. Choices are: [...]

In both versions

Potential.objects.filter(community=Place.objects.first())

works fine.

It's not clear to me from the docs whether the 1.10 behavior was intended, though.

It appears that adding the lines

                elif getattr(self.lhs.field.target_field, 'primary_key', False):
                    target_field = 'pk'

to django/db/models/fields/related_lookups.py at line 92 fixes this problem.

Change History (7)

comment:1 Changed 2 years ago by Daniel Keller

Description: modified (diff)

comment:2 Changed 2 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:3 Changed 2 years ago by felixxm

Cc: felixxm added

comment:4 Changed 2 years ago by Simon Charette

Cc: Simon Charette added

I find it a bit odd that we allow Place to be used when Community is expected (Community is a Place but not the other way around) but since this was allowed before we should fix that.

comment:5 Changed 2 years ago by Tim Graham

Has patch: set

comment:6 Changed 2 years ago by GitHub <noreply@…>

Resolution: fixed
Status: newclosed

In d66378a8:

Fixed #28175 -- Fixed in lookups on a foreign key when using the foreign key's parent model as the lookup value.

Thanks Simon Charette for review.

comment:7 Changed 2 years ago by Tim Graham <timograham@…>

In f9a45933:

[1.11.x] Fixed #28175 -- Fixed in lookups on a foreign key when using the foreign key's parent model as the lookup value.

Thanks Simon Charette for review.

Backport of d66378a8b2d48d41b56ba3622189493e4edf9e5a from master

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