[1.11.x] Fixed #28101 -- Fixed a regression with nested __in subquery lookups and to_field.

Thanks Kristian Klette for the report and Tim for the help.

Backport of 8ef35468b6 from master
This commit is contained in:
Simon Charette 2017-04-22 17:04:02 -04:00
parent 395df007f4
commit 0ad1693494
4 changed files with 26 additions and 5 deletions

View File

@ -339,6 +339,7 @@ class Query(object):
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):

View File

@ -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`).

View File

@ -78,7 +78,7 @@ class Annotation(models.Model):
@python_2_unicode_compatible
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:
@ -126,6 +126,10 @@ class Report(models.Model):
return self.name
class ReportComment(models.Model):
report = models.ForeignKey(Report, models.CASCADE)
@python_2_unicode_compatible
class Ranking(models.Model):
rank = models.IntegerField()

View File

@ -26,10 +26,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, Ticket21203Child, Ticket21203Parent,
Ticket23605A, Ticket23605B, Ticket23605C, TvChef, Valid, X,
RelatedIndividual, RelatedObject, Report, ReportComment, ReservedName,
Responsibility, School, SharedConnection, SimpleCategory, SingleObject,
SpecialCategory, Staff, StaffUser, Student, Tag, Task, Ticket21203Child,
Ticket21203Parent, Ticket23605A, Ticket23605B, Ticket23605C, TvChef, Valid,
X,
)
@ -2420,6 +2421,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")