summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2017-10-16 11:10:21 -0400
committerTim Graham <timograham@gmail.com>2017-10-16 13:56:38 -0400
commit1b73ccc4bf78af905f72f4658cf463f38ebf7b97 (patch)
tree5fb10a733542297ab5adbe52a04ca3ffc5f1abaa /django
parent61a6245dc5c9ad8423dcceb47fdeff167c838542 (diff)
Fixed #28497 -- Restored the ability to use sliced QuerySets with __exact.
Regression in ec50937bcbe160e658ef881021402e156beb0eaf. Thanks Simon Charette for review.
Diffstat (limited to 'django')
-rw-r--r--django/db/models/lookups.py14
-rw-r--r--django/db/models/sql/query.py3
2 files changed, 17 insertions, 0 deletions
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index 66945c4a1a..a9abd82cd9 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -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):
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index a962aabdf1..372431c620 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -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.