1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #20939 -- Simplified query generation by converting QuerySet to Query.

Thanks Anssi Kääriäinen for the initial patch and Anssi, Simon Charette,
and Josh Smeaton for review.
This commit is contained in:
Tim Graham
2016-10-28 11:20:23 -04:00
committed by GitHub
parent 80e742d991
commit 1bc249c2a6
9 changed files with 71 additions and 140 deletions

View File

@@ -81,27 +81,18 @@ class RelatedIn(In):
AND)
return root_constraint.as_sql(compiler, connection)
else:
if getattr(self.rhs, '_forced_pk', False):
self.rhs.clear_select_clause()
if getattr(self.lhs.output_field, 'primary_key', False):
# A case like Restaurant.objects.filter(place__in=restaurant_qs),
# where place is a OneToOneField and the primary key of
# Restaurant.
target_field = self.lhs.field.name
else:
target_field = self.lhs.field.target_field.name
self.rhs.add_fields([target_field], True)
return super(RelatedIn, self).as_sql(compiler, connection)
def __getstate__(self):
"""
Prevent pickling a query with an __in=inner_qs lookup from evaluating
inner_qs.
"""
from django.db.models.query import QuerySet # Avoid circular import
state = self.__dict__.copy()
if isinstance(self.rhs, QuerySet):
state['rhs'] = (self.rhs.__class__, self.rhs.query)
return state
def __setstate__(self, state):
self.__dict__.update(state)
if isinstance(self.rhs, tuple):
queryset_class, query = self.rhs
queryset = queryset_class()
queryset.query = query
self.rhs = queryset
class RelatedLookupMixin(object):
def get_prep_lookup(self):