From 8ef35468b660e1c25af67a8299202b8bc108679f Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 22 Apr 2017 17:04:02 -0400 Subject: [PATCH] Fixed #28101 -- Fixed a regression with nested __in subquery lookups and to_field. Thanks Kristian Klette for the report and Tim for the help. --- django/db/models/sql/query.py | 1 + docs/releases/1.11.1.txt | 3 +++ tests/queries/models.py | 6 +++++- tests/queries/tests.py | 22 +++++++++++++++++----- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 3d0e0fa95b..6bfd454080 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -326,6 +326,7 @@ class Query: if hasattr(obj, '_setup_query'): obj._setup_query() obj.context = self.context.copy() + obj._forced_pk = getattr(self, '_forced_pk', False) return obj def add_context(self, key, value): diff --git a/docs/releases/1.11.1.txt b/docs/releases/1.11.1.txt index 1ce96347d7..78715791e7 100644 --- a/docs/releases/1.11.1.txt +++ b/docs/releases/1.11.1.txt @@ -52,3 +52,6 @@ Bugfixes * Corrected the stack level of unordered queryset pagination warnings (:ticket:`28109`). + +* Fixed a regression causing incorrect queries for ``__in`` subquery lookups + when models use ``ForeignKey.to_field`` (:ticket:`28101`). diff --git a/tests/queries/models.py b/tests/queries/models.py index 797d755002..c3f5125498 100644 --- a/tests/queries/models.py +++ b/tests/queries/models.py @@ -69,7 +69,7 @@ class Annotation(models.Model): class ExtraInfo(models.Model): 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) class Meta: @@ -114,6 +114,10 @@ class Report(models.Model): return self.name +class ReportComment(models.Model): + report = models.ForeignKey(Report, models.CASCADE) + + class Ranking(models.Model): rank = models.IntegerField() author = models.ForeignKey(Author, models.CASCADE) diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 576abc8e24..8bcf38f146 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -22,11 +22,11 @@ from .models import ( Note, NullableName, Number, ObjectA, ObjectB, ObjectC, OneToOneCategory, Order, OrderItem, Page, Paragraph, Person, Plaything, PointerA, Program, ProxyCategory, ProxyObjectA, ProxyObjectB, Ranking, Related, - RelatedIndividual, RelatedObject, Report, ReservedName, Responsibility, - School, SharedConnection, SimpleCategory, SingleObject, SpecialCategory, - Staff, StaffUser, Student, Tag, Task, Teacher, Ticket21203Child, - Ticket21203Parent, Ticket23605A, Ticket23605B, Ticket23605C, TvChef, Valid, - X, + RelatedIndividual, RelatedObject, Report, ReportComment, ReservedName, + Responsibility, School, SharedConnection, SimpleCategory, SingleObject, + SpecialCategory, Staff, StaffUser, Student, Tag, Task, Teacher, + Ticket21203Child, Ticket21203Parent, Ticket23605A, Ticket23605B, + Ticket23605C, TvChef, Valid, X, ) @@ -2406,6 +2406,18 @@ class ToFieldTests(TestCase): {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): apple = Food.objects.create(name="apple") pear = Food.objects.create(name="pear")