Opened 7 years ago

Closed 7 years ago

Last modified 7 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: Mariusz Felisiak, 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 by Daniel Keller, 7 years ago

Description: modified (diff)

comment:2 by Tim Graham, 7 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:3 by Mariusz Felisiak, 7 years ago

Cc: Mariusz Felisiak added

comment:4 by Simon Charette, 7 years ago

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 by Tim Graham, 7 years ago

Has patch: set

comment:6 by GitHub <noreply@…>, 7 years ago

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 by Tim Graham <timograham@…>, 7 years ago

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