Skip to content

Commit b524c2f

Browse files
committed
make ObjectIdAutoField.to_python() accept integers as strings
1 parent 4bd90c7 commit b524c2f

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
@@ -40,11 +40,14 @@ def to_python(self, value):
4040
try:
4141
return ObjectId(value)
4242
except errors.InvalidId:
43-
raise exceptions.ValidationError(
44-
self.error_messages["invalid"],
45-
code="invalid",
46-
params={"value": value},
47-
) from None
43+
try:
44+
return int(value)
45+
except ValueError:
46+
raise exceptions.ValidationError(
47+
self.error_messages["invalid"],
48+
code="invalid",
49+
params={"value": value},
50+
) from None
4851

4952
@cached_property
5053
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 ObjectIdAutoFieldTests(SimpleTestCase):
7+
def test_to_python(self):
8+
f = ObjectIdAutoField()
9+
self.assertEqual(f.to_python("1"), 1)

0 commit comments

Comments
 (0)