Skip to content

Commit f35b419

Browse files
committed
make ObjectIdAutoField.to_python() accept integers as strings
1 parent 930ad84 commit f35b419

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8282
# https://github.com/mongodb-labs/django-mongodb/issues/161
8383
"queries.tests.RelatedLookupTypeTests.test_values_queryset_lookup",
8484
"queries.tests.ValuesSubqueryTests.test_values_in_subquery",
85-
# ObjectIdAutoField.to_python() doesn't accept integers as strings.
86-
"model_formsets.tests.ModelFormsetTest.test_inline_formsets_with_custom_save_method",
8785
}
8886
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
8987
_django_test_expected_failures_bitwise = {

django_mongodb/fields/auto.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,14 @@ def to_python(self, value):
4343
try:
4444
return ObjectId(value)
4545
except errors.InvalidId:
46-
raise exceptions.ValidationError(
47-
self.error_messages["invalid"],
48-
code="invalid",
49-
params={"value": value},
50-
) from None
46+
try:
47+
return int(value)
48+
except ValueError:
49+
raise exceptions.ValidationError(
50+
self.error_messages["invalid"],
51+
code="invalid",
52+
params={"value": value},
53+
) from None
5154

5255
@cached_property
5356
def validators(self):

tests/model_fields_/__init__.py

Whitespace-only changes.

tests/model_fields_/test_autofield.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.test import SimpleTestCase
2+
3+
from django_mongodb.fields import ObjectIdAutoField
4+
5+
6+
class MethodTests(SimpleTestCase):
7+
def test_to_python(self):
8+
f = ObjectIdAutoField()
9+
self.assertEqual(f.to_python("1"), 1)

0 commit comments

Comments
 (0)