diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-07-25 20:45:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-25 20:45:55 +0200 |
| commit | 806ba19bbff311b7d567857ae61db6ff84af4a2c (patch) | |
| tree | e803ce13afc283fe1d197b7d97ddb78d8f25f9e8 /django/db/models/sql | |
| parent | 1853383969a4c53bbeba998757c30410bd3df4bb (diff) | |
Added Query.is_sliced property.
Previously, we used Query.can_filter() mainly to check if a query is
sliced what was confusing.
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/query.py | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index e15e64cde4..0369e01348 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -413,7 +413,6 @@ class Query(BaseExpression): """ if not self.annotation_select: return {} - has_limit = self.low_mark != 0 or self.high_mark is not None existing_annotations = [ annotation for alias, annotation in self.annotations.items() @@ -430,7 +429,7 @@ class Query(BaseExpression): # those operations must be done in a subquery so that the query # aggregates on the limit and/or distinct results instead of applying # the distinct and limit after the aggregation. - if (isinstance(self.group_by, tuple) or has_limit or existing_annotations or + if (isinstance(self.group_by, tuple) or self.is_sliced or existing_annotations or self.distinct or self.combinator): from django.db.models.sql.subqueries import AggregateQuery outer_query = AggregateQuery(self.model) @@ -438,7 +437,7 @@ class Query(BaseExpression): inner_query.select_for_update = False inner_query.select_related = False inner_query.set_annotation_mask(self.annotation_select) - if not has_limit and not self.distinct_fields: + if not self.is_sliced and not self.distinct_fields: # Queries with distinct_fields need ordering and when a limit # is applied we must take the slice from the ordered query. # Otherwise no need for ordering. @@ -548,7 +547,7 @@ class Query(BaseExpression): """ assert self.model == rhs.model, \ "Cannot combine queries on two different base models." - assert self.can_filter(), \ + assert not self.is_sliced, \ "Cannot combine queries once a slice has been taken." assert self.distinct == rhs.distinct, \ "Cannot combine a unique query with a non-unique query." @@ -1762,6 +1761,10 @@ class Query(BaseExpression): """Clear any existing limits.""" self.low_mark, self.high_mark = 0, None + @property + def is_sliced(self): + return self.low_mark != 0 or self.high_mark is not None + def has_limit_one(self): return self.high_mark is not None and (self.high_mark - self.low_mark) == 1 @@ -1771,7 +1774,7 @@ class Query(BaseExpression): Typically, this means no limits or offsets have been put on the results. """ - return not self.low_mark and self.high_mark is None + return not self.is_sliced def clear_select_clause(self): """Remove all fields from SELECT clause.""" |
