Skip to content

Commit d1f48c5

Browse files
committed
add NotSupportedError for QuerySet.annotate()
1 parent 05aef78 commit d1f48c5

File tree

5 files changed

+86
-12
lines changed

5 files changed

+86
-12
lines changed

.github/workflows/test-python.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ jobs:
7676
defer
7777
defer_regress
7878
from_db_value
79-
lookup.tests.LookupTests.test_escaping
80-
lookup.tests.LookupTests.test_isnull_textfield
81-
lookup.tests.LookupQueryingTests.test_isnull_lookup_in_filter
79+
lookup
8280
model_fields
8381
or_lookups
8482
sessions_tests

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ DATABASES = {
4747
## Known issues and limitations
4848

4949
- The following `QuerySet` methods aren't supported:
50+
- `annotate()`
5051
- `aggregate()`
52+
- `dates()`
53+
- `datetimes()`
5154
- `distinct()`
5255
- `extra()`
5356
- `select_related()`

django_mongodb/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class DatabaseWrapper(BaseDatabaseWrapper):
5050
"TimeField": "date",
5151
"UUIDField": "string",
5252
}
53+
operators = {
54+
"exact": "= %s",
55+
"gt": "> %s",
56+
"gte": ">= %s",
57+
"lt": "< %s",
58+
"lte": "<= %s",
59+
}
5360

5461
display_name = "MongoDB"
5562
vendor = "mongodb"

django_mongodb/compiler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
NotSupportedError,
77
connections,
88
)
9-
from django.db.models import NOT_PROVIDED, Count, Expression
9+
from django.db.models import NOT_PROVIDED, Count, Expression, Value
1010
from django.db.models.aggregates import Aggregate
1111
from django.db.models.constants import LOOKUP_SEP
1212
from django.db.models.sql import compiler
@@ -97,6 +97,12 @@ def check_query(self):
9797
"""Check if the current query is supported by the database."""
9898
if self.query.is_empty():
9999
raise EmptyResultSet()
100+
# Supported annotations are Exists() and Count().
101+
if self.query.annotations and self.query.annotations not in (
102+
{"a": Value(1)},
103+
{"__count": Count("*")},
104+
):
105+
raise NotSupportedError("QuerySet.annotate() is not supported on MongoDB.")
100106
if self.query.distinct:
101107
# This is a heuristic to detect QuerySet.datetimes() and dates().
102108
# "datetimefield" and "datefield" are the names of the annotations

django_mongodb/features.py

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
class DatabaseFeatures(BaseDatabaseFeatures):
5+
supports_date_lookup_using_string = False
56
supports_foreign_keys = False
67
# Not implemented: https://github.com/mongodb-labs/django-mongodb/issues/8
78
supports_json_field = False
@@ -20,28 +21,45 @@ class DatabaseFeatures(BaseDatabaseFeatures):
2021
"basic.tests.ModelLookupTest.test_rich_lookup",
2122
"basic.tests.ModelLookupTest.test_too_many",
2223
"basic.tests.ModelTest.test_year_lookup_edge_case",
24+
"lookup.tests.LookupTests.test_chain_date_time_lookups",
25+
"lookup.test_timefield.TimeFieldLookupTests.test_hour_lookups",
26+
"lookup.test_timefield.TimeFieldLookupTests.test_minute_lookups",
27+
"lookup.test_timefield.TimeFieldLookupTests.test_second_lookups",
2328
"timezones.tests.LegacyDatabaseTests.test_query_datetime_lookups",
2429
"timezones.tests.NewDatabaseTests.test_query_convert_timezones",
2530
"timezones.tests.NewDatabaseTests.test_query_datetime_lookups",
2631
"timezones.tests.NewDatabaseTests.test_query_datetime_lookups_in_other_timezone",
32+
# 'NulledTransform' object has no attribute 'alias'
33+
"lookup.tests.LookupTests.test_exact_none_transform",
2734
# "Save with update_fields did not affect any rows."
2835
"basic.tests.SelectOnSaveTests.test_select_on_save_lying_update",
29-
# QuerySet.extra() not supported.
30-
"basic.tests.ModelTest.test_extra_method_select_argument_with_dashes",
31-
"basic.tests.ModelTest.test_extra_method_select_argument_with_dashes_and_values",
32-
# QuerySet.aggregate() not supported: https://github.com/mongodb-labs/django-mongodb/issues/12
33-
"from_db_value.tests.FromDBValueTest.test_aggregation",
34-
"timezones.tests.LegacyDatabaseTests.test_query_aggregation",
35-
"timezones.tests.NewDatabaseTests.test_query_aggregation",
3636
# filtering on large decimalfield, see https://code.djangoproject.com/ticket/34590
3737
# for some background.
3838
"model_fields.test_decimalfield.DecimalFieldTests.test_lookup_decimal_larger_than_max_digits",
3939
"model_fields.test_decimalfield.DecimalFieldTests.test_lookup_really_big_value",
4040
# 'TruncDate' object has no attribute 'alias'
4141
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz",
4242
"model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz",
43-
# Empty queryset ORed (|) with another gives empty results.
43+
# Incorrect empty QuerySet handling: https://github.com/mongodb-labs/django-mongodb/issues/22
44+
"lookup.tests.LookupTests.test_in",
4445
"or_lookups.tests.OrLookupsTests.test_empty_in",
46+
# Slicing with QuerySet.count() doesn't work.
47+
"lookup.tests.LookupTests.test_count",
48+
# Custom lookups not supported.
49+
"lookup.tests.LookupTests.test_custom_lookup_none_rhs",
50+
# Lookup in order_by() not supported: argument of type 'LessThan' is not iterable
51+
"lookup.tests.LookupQueryingTests.test_lookup_in_order_by",
52+
# annotate() after values() doesn't raise NotSupportedError.
53+
"lookup.tests.LookupTests.test_exact_query_rhs_with_selected_columns",
54+
# tuple index out of range in _normalize_lookup_value()
55+
"lookup.tests.LookupTests.test_exact_sliced_queryset_limit_one",
56+
"lookup.tests.LookupTests.test_exact_sliced_queryset_limit_one_offset",
57+
# Regex lookup doesn't work on non-string fields.
58+
"lookup.tests.LookupTests.test_regex_non_string",
59+
# Substr not implemented.
60+
"lookup.tests.LookupTests.test_pattern_lookups_with_substr",
61+
# Querying ObjectID with string doesn't work.
62+
"lookup.tests.LookupTests.test_lookup_int_as_str",
4563
}
4664

4765
django_test_skips = {
@@ -64,6 +82,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6482
"update.tests.AdvancedTests.test_update_transformed_field",
6583
},
6684
"AutoField not supported.": {
85+
"lookup.tests.LookupTests.test_in_ignore_none_with_unhashable_items",
6786
"model_fields.test_autofield.AutoFieldTests",
6887
"model_fields.test_autofield.BigAutoFieldTests",
6988
"model_fields.test_autofield.SmallAutoFieldTests",
@@ -104,6 +123,36 @@ class DatabaseFeatures(BaseDatabaseFeatures):
104123
"Test assumes integer primary key.": {
105124
"model_fields.test_foreignkey.ForeignKeyTests.test_to_python",
106125
},
126+
# https://github.com/mongodb-labs/django-mongodb/issues/12
127+
"QuerySet.aggregate() not supported.": {
128+
"lookup.tests.LookupQueryingTests.test_aggregate_combined_lookup",
129+
"from_db_value.tests.FromDBValueTest.test_aggregation",
130+
"timezones.tests.LegacyDatabaseTests.test_query_aggregation",
131+
"timezones.tests.NewDatabaseTests.test_query_aggregation",
132+
},
133+
"QuerySet.annotate() not supported.": {
134+
"lookup.test_decimalfield.DecimalFieldLookupTests",
135+
"lookup.tests.LookupTests.test_exact_exists",
136+
"lookup.tests.LookupTests.test_nested_outerref_lhs",
137+
"lookup.tests.LookupQueryingTests.test_alias",
138+
"lookup.tests.LookupQueryingTests.test_annotate",
139+
"lookup.tests.LookupQueryingTests.test_annotate_field_greater_than_field",
140+
"lookup.tests.LookupQueryingTests.test_annotate_field_greater_than_literal",
141+
"lookup.tests.LookupQueryingTests.test_annotate_field_greater_than_value",
142+
"lookup.tests.LookupQueryingTests.test_annotate_greater_than_or_equal",
143+
"lookup.tests.LookupQueryingTests.test_annotate_greater_than_or_equal_float",
144+
"lookup.tests.LookupQueryingTests.test_annotate_less_than_float",
145+
"lookup.tests.LookupQueryingTests.test_annotate_literal_greater_than_field",
146+
"lookup.tests.LookupQueryingTests.test_annotate_value_greater_than_value",
147+
"lookup.tests.LookupQueryingTests.test_combined_annotated_lookups_in_filter",
148+
"lookup.tests.LookupQueryingTests.test_combined_annotated_lookups_in_filter_false",
149+
"lookup.tests.LookupQueryingTests.test_combined_lookups",
150+
"lookup.tests.LookupQueryingTests.test_conditional_expression",
151+
"lookup.tests.LookupQueryingTests.test_filter_exists_lhs",
152+
"lookup.tests.LookupQueryingTests.test_filter_lookup_lhs",
153+
"lookup.tests.LookupQueryingTests.test_filter_subquery_lhs",
154+
"lookup.tests.LookupQueryingTests.test_filter_wrapped_lookup_lhs",
155+
},
107156
"QuerySet.dates() is not supported on MongoDB.": {
108157
"dates.tests.DatesTests.test_dates_trunc_datetime_fields",
109158
"dates.tests.DatesTests.test_related_model_traverse",
@@ -124,6 +173,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
124173
"basic.tests.ModelTest.test_extra_method_select_argument_with_dashes",
125174
"basic.tests.ModelTest.test_extra_method_select_argument_with_dashes_and_values",
126175
"defer.tests.DeferTests.test_defer_extra",
176+
"lookup.tests.LookupTests.test_values",
177+
"lookup.tests.LookupTests.test_values_list",
127178
},
128179
"Queries with multiple tables are not supported.": {
129180
"defer.tests.BigChildDeferTests.test_defer_baseclass_when_subclass_has_added_field",
@@ -136,6 +187,11 @@ class DatabaseFeatures(BaseDatabaseFeatures):
136187
"defer.tests.DeferTests.test_only_baseclass_when_subclass_has_no_added_fields",
137188
"defer.tests.TestDefer2.test_defer_inheritance_pk_chaining",
138189
"defer_regress.tests.DeferRegressionTest.test_ticket_16409",
190+
"lookup.tests.LookupQueryingTests.test_multivalued_join_reuse",
191+
"lookup.tests.LookupTests.test_filter_by_reverse_related_field_transform",
192+
"lookup.tests.LookupTests.test_lookup_collision",
193+
"lookup.tests.LookupTests.test_lookup_rhs",
194+
"lookup.tests.LookupTests.test_isnull_non_boolean_value",
139195
"model_fields.test_manytomanyfield.ManyToManyFieldDBTests.test_value_from_object_instance_with_pk",
140196
"model_fields.test_uuid.TestAsPrimaryKey.test_two_level_foreign_keys",
141197
"timezones.tests.LegacyDatabaseTests.test_query_annotation",
@@ -149,6 +205,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
149205
"update.tests.SimpleTest.test_foreign_key_update_with_id",
150206
"update.tests.SimpleTest.test_nonempty_update_with_inheritance",
151207
},
208+
"Test inspects query for SQL": {
209+
"lookup.tests.LookupTests.test_in_ignore_none",
210+
"lookup.tests.LookupTests.test_textfield_exact_null",
211+
},
152212
"Test executes raw SQL.": {
153213
"timezones.tests.LegacyDatabaseTests.test_cursor_execute_accepts_naive_datetime",
154214
"timezones.tests.LegacyDatabaseTests.test_cursor_execute_returns_naive_datetime",

0 commit comments

Comments
 (0)