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:
@@ -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):
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user