File tree Expand file tree Collapse file tree 3 files changed +18
-6
lines changed Expand file tree Collapse file tree 3 files changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -126,7 +126,17 @@ def get_count(self, check_exists=False):
126
126
127
127
If `check_exists` is True, only check if any object matches.
128
128
"""
129
- kwargs = {"limit" : 1 } if check_exists else {}
129
+ kwargs = {}
130
+ # If this query is sliced, the limits will be set on the subquery.
131
+ inner_query = getattr (self .query , "inner_query" , None )
132
+ low_mark = inner_query .low_mark if inner_query else 0
133
+ high_mark = inner_query .high_mark if inner_query else None
134
+ if low_mark > 0 :
135
+ kwargs ["skip" ] = low_mark
136
+ if check_exists :
137
+ kwargs ["limit" ] = 1
138
+ elif high_mark is not None :
139
+ kwargs ["limit" ] = high_mark - low_mark
130
140
try :
131
141
return self .build_query ().count (** kwargs )
132
142
except EmptyResultSet :
Original file line number Diff line number Diff line change @@ -26,8 +26,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
26
26
"lookup.tests.LookupTests.test_exact_none_transform" ,
27
27
# "Save with update_fields did not affect any rows."
28
28
"basic.tests.SelectOnSaveTests.test_select_on_save_lying_update" ,
29
- # Slicing with QuerySet.count() doesn't work.
30
- "lookup.tests.LookupTests.test_count" ,
31
29
# Lookup in order_by() not supported:
32
30
# argument of type '<database function>' is not iterable
33
31
"db_functions.comparison.test_coalesce.CoalesceTests.test_ordering" ,
Original file line number Diff line number Diff line change @@ -51,12 +51,16 @@ def fetch(self):
51
51
yield from self .get_cursor ()
52
52
53
53
@wrap_database_errors
54
- def count (self , limit = None ):
54
+ def count (self , limit = None , skip = None ):
55
55
"""
56
56
Return the number of objects that would be returned, if this query was
57
- executed, up to `limit`.
57
+ executed, up to `limit`, skipping `skip` .
58
58
"""
59
- kwargs = {"limit" : limit } if limit is not None else {}
59
+ kwargs = {}
60
+ if limit is not None :
61
+ kwargs ["limit" ] = limit
62
+ if skip is not None :
63
+ kwargs ["skip" ] = skip
60
64
return self .collection .count_documents (self .mongo_query , ** kwargs )
61
65
62
66
def order_by (self , ordering ):
You can’t perform that action at this time.
0 commit comments