Skip to content

Commit 5e98655

Browse files
committed
prohibit int in ObjectIdAutoField
1 parent f5dbacb commit 5e98655

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

.github/workflows/test-python.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
uses: actions/checkout@v4
3434
with:
3535
repository: 'mongodb-forks/django'
36-
ref: 'mongodb-5.1.x'
36+
ref: 'objectid-no-int'
3737
path: 'django_repo'
3838
persist-credentials: false
3939
- name: Install system packages for Django's Python test dependencies

django_mongodb_backend/features.py

+5
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ def django_test_expected_failures(self):
205205
"lookup.tests.LookupTests.test_in_ignore_none_with_unhashable_items",
206206
"m2m_through_regress.tests.ThroughLoadDataTestCase.test_sequence_creation",
207207
"many_to_many.tests.ManyToManyTests.test_add_remove_invalid_type",
208+
"many_to_one.tests.ManyToOneTests.test_fk_to_smallautofield",
209+
"many_to_one.tests.ManyToOneTests.test_fk_to_bigautofield",
208210
"migrations.test_operations.OperationTests.test_autofield__bigautofield_foreignfield_growth",
209211
"migrations.test_operations.OperationTests.test_model_with_bigautofield",
210212
"migrations.test_operations.OperationTests.test_smallfield_autofield_foreignfield_growth",
@@ -213,6 +215,8 @@ def django_test_expected_failures(self):
213215
"model_fields.test_autofield.BigAutoFieldTests",
214216
"model_fields.test_autofield.SmallAutoFieldTests",
215217
"queries.tests.TestInvalidValuesRelation.test_invalid_values",
218+
"schema.tests.SchemaTests.test_alter_autofield_pk_to_bigautofield_pk",
219+
"schema.tests.SchemaTests.test_alter_autofield_pk_to_smallautofield_pk",
216220
},
217221
"Converters aren't run on returning fields from insert.": {
218222
# Unsure this is needed for this backend. Can implement by request.
@@ -231,6 +235,7 @@ def django_test_expected_failures(self):
231235
"queries.test_qs_combinators.QuerySetSetOperationTests.test_order_raises_on_non_selected_column",
232236
"queries.tests.RelatedLookupTypeTests.test_values_queryset_lookup",
233237
"queries.tests.ValuesSubqueryTests.test_values_in_subquery",
238+
"sites_tests.tests.CreateDefaultSiteTests.test_no_site_id",
234239
},
235240
"Cannot use QuerySet.delete() when querying across multiple collections on MongoDB.": {
236241
"admin_changelist.tests.ChangeListTests.test_distinct_for_many_to_many_at_second_level_in_search_fields",

django_mongodb_backend/fields/auto.py

+6-17
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,11 @@ def deconstruct(self):
2222
return name, path, args, kwargs
2323

2424
def get_prep_value(self, value):
25-
if value is None:
26-
return None
27-
# Accept int for compatibility with Django's test suite which has many
28-
# instances of manually assigned integer IDs, as well as for things
29-
# like settings.SITE_ID which has a system check requiring an integer.
30-
if isinstance(value, (ObjectId | int)):
25+
if value is None or isinstance(value, ObjectId):
3126
return value
3227
try:
3328
return ObjectId(value)
3429
except errors.InvalidId as e:
35-
# A manually assigned integer ID?
36-
if isinstance(value, str) and value.isdigit():
37-
return int(value)
3830
raise ValueError(f"Field '{self.name}' expected an ObjectId but got {value!r}.") from e
3931

4032
def get_internal_type(self):
@@ -46,14 +38,11 @@ def to_python(self, value):
4638
try:
4739
return ObjectId(value)
4840
except errors.InvalidId:
49-
try:
50-
return int(value)
51-
except ValueError:
52-
raise exceptions.ValidationError(
53-
self.error_messages["invalid"],
54-
code="invalid",
55-
params={"value": value},
56-
) from None
41+
raise exceptions.ValidationError(
42+
self.error_messages["invalid"],
43+
code="invalid",
44+
params={"value": value},
45+
) from None
5746

5847
@cached_property
5948
def validators(self):

tests/indexes_/test_condition.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_negated_not_supported(self):
3434
Index(
3535
name="test",
3636
fields=["headline"],
37-
condition=~Q(pk=True),
37+
condition=~Q(pk__isnull=True),
3838
)._get_condition_mql(Article, schema_editor=editor)
3939

4040
def test_xor_not_supported(self):
@@ -43,7 +43,7 @@ def test_xor_not_supported(self):
4343
Index(
4444
name="test",
4545
fields=["headline"],
46-
condition=Q(pk=True) ^ Q(pk=False),
46+
condition=Q(pk__isnull=True) ^ Q(pk__isnull=False),
4747
)._get_condition_mql(Article, schema_editor=editor)
4848

4949
def test_operations(self):

tests/model_fields_/test_autofield.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.core.exceptions import ValidationError
12
from django.test import SimpleTestCase
23

34
from django_mongodb_backend.fields import ObjectIdAutoField
@@ -17,4 +18,6 @@ def test_get_internal_type(self):
1718

1819
def test_to_python(self):
1920
f = ObjectIdAutoField()
20-
self.assertEqual(f.to_python("1"), 1)
21+
msg = "“1” is not a valid Object Id."
22+
with self.assertRaisesMessage(ValidationError, msg):
23+
f.to_python("1")

0 commit comments

Comments
 (0)