Fixed #28101 -- Fixed a regression with nested __in subquery lookups and to_field.

Thanks Kristian Klette for the report and Tim for the help.
This commit is contained in:
Simon Charette 2017-04-22 17:04:02 -04:00
parent 3f1ba76851
commit 8ef35468b6
4 changed files with 26 additions and 6 deletions

View File

@ -326,6 +326,7 @@ class Query:
if hasattr(obj, '_setup_query'): if hasattr(obj, '_setup_query'):
obj._setup_query() obj._setup_query()
obj.context = self.context.copy() obj.context = self.context.copy()
obj._forced_pk = getattr(self, '_forced_pk', False)
return obj return obj
def add_context(self, key, value): def add_context(self, key, value):

View File

@ -52,3 +52,6 @@ Bugfixes
* Corrected the stack level of unordered queryset pagination warnings * Corrected the stack level of unordered queryset pagination warnings
(:ticket:`28109`). (:ticket:`28109`).
* Fixed a regression causing incorrect queries for ``__in`` subquery lookups
when models use ``ForeignKey.to_field`` (:ticket:`28101`).

View File

@ -69,7 +69,7 @@ class Annotation(models.Model):
class ExtraInfo(models.Model): class ExtraInfo(models.Model):
info = models.CharField(max_length=100) info = models.CharField(max_length=100)
note = models.ForeignKey(Note, models.CASCADE) note = models.ForeignKey(Note, models.CASCADE, null=True)
value = models.IntegerField(null=True) value = models.IntegerField(null=True)
class Meta: class Meta:
@ -114,6 +114,10 @@ class Report(models.Model):
return self.name return self.name
class ReportComment(models.Model):
report = models.ForeignKey(Report, models.CASCADE)
class Ranking(models.Model): class Ranking(models.Model):
rank = models.IntegerField() rank = models.IntegerField()
author = models.ForeignKey(Author, models.CASCADE) author = models.ForeignKey(Author, models.CASCADE)

View File

@ -22,11 +22,11 @@ from .models import (
Note, NullableName, Number, ObjectA, ObjectB, ObjectC, OneToOneCategory, Note, NullableName, Number, ObjectA, ObjectB, ObjectC, OneToOneCategory,
Order, OrderItem, Page, Paragraph, Person, Plaything, PointerA, Program, Order, OrderItem, Page, Paragraph, Person, Plaything, PointerA, Program,
ProxyCategory, ProxyObjectA, ProxyObjectB, Ranking, Related, ProxyCategory, ProxyObjectA, ProxyObjectB, Ranking, Related,
RelatedIndividual, RelatedObject, Report, ReservedName, Responsibility, RelatedIndividual, RelatedObject, Report, ReportComment, ReservedName,
School, SharedConnection, SimpleCategory, SingleObject, SpecialCategory, Responsibility, School, SharedConnection, SimpleCategory, SingleObject,
Staff, StaffUser, Student, Tag, Task, Teacher, Ticket21203Child, SpecialCategory, Staff, StaffUser, Student, Tag, Task, Teacher,
Ticket21203Parent, Ticket23605A, Ticket23605B, Ticket23605C, TvChef, Valid, Ticket21203Child, Ticket21203Parent, Ticket23605A, Ticket23605B,
X, Ticket23605C, TvChef, Valid, X,
) )
@ -2406,6 +2406,18 @@ class ToFieldTests(TestCase):
{apple} {apple}
) )
def test_nested_in_subquery(self):
extra = ExtraInfo.objects.create()
author = Author.objects.create(num=42, extra=extra)
report = Report.objects.create(creator=author)
comment = ReportComment.objects.create(report=report)
comments = ReportComment.objects.filter(
report__in=Report.objects.filter(
creator__in=extra.author_set.all(),
),
)
self.assertSequenceEqual(comments, [comment])
def test_reverse_in(self): def test_reverse_in(self):
apple = Food.objects.create(name="apple") apple = Food.objects.create(name="apple")
pear = Food.objects.create(name="pear") pear = Food.objects.create(name="pear")