1
0
mirror of https://github.com/django/django.git synced 2025-10-26 15:16:09 +00:00

Fixed #28497 -- Restored the ability to use sliced QuerySets with __exact.

Regression in ec50937bcb.

Thanks Simon Charette for review.
This commit is contained in:
Tim Graham
2017-10-16 11:10:21 -04:00
parent 61a6245dc5
commit 1b73ccc4bf
3 changed files with 39 additions and 0 deletions

View File

@@ -245,6 +245,20 @@ class FieldGetDbPrepValueIterableMixin(FieldGetDbPrepValueMixin):
class Exact(FieldGetDbPrepValueMixin, BuiltinLookup):
lookup_name = 'exact'
def process_rhs(self, compiler, connection):
from django.db.models.sql.query import Query
if isinstance(self.rhs, Query):
if self.rhs.has_limit_one():
# The subquery must select only the pk.
self.rhs.clear_select_clause()
self.rhs.add_fields(['pk'])
else:
raise ValueError(
'The QuerySet value for an exact lookup must be limited to '
'one result using slicing.'
)
return super().process_rhs(compiler, connection)
@Field.register_lookup
class IExact(BuiltinLookup):

View File

@@ -1627,6 +1627,9 @@ class Query:
"""Clear any existing limits."""
self.low_mark, self.high_mark = 0, None
def has_limit_one(self):
return self.high_mark is not None and (self.high_mark - self.low_mark) == 1
def can_filter(self):
"""
Return True if adding filters to this instance is still possible.