diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index befc7a0f3..1fd0c1fce 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -93,6 +93,7 @@ jobs: migrations model_fields model_forms + model_formsets model_inheritance_regress mutually_referential nested_foreign_keys diff --git a/django_mongodb/fields/auto.py b/django_mongodb/fields/auto.py index 34dc3a449..e77d898ff 100644 --- a/django_mongodb/fields/auto.py +++ b/django_mongodb/fields/auto.py @@ -40,11 +40,14 @@ def to_python(self, value): try: return ObjectId(value) except errors.InvalidId: - raise exceptions.ValidationError( - self.error_messages["invalid"], - code="invalid", - params={"value": value}, - ) from None + try: + return int(value) + except ValueError: + raise exceptions.ValidationError( + self.error_messages["invalid"], + code="invalid", + params={"value": value}, + ) from None @cached_property def validators(self): diff --git a/tests/model_fields_/__init__.py b/tests/model_fields_/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/model_fields_/test_autofield.py b/tests/model_fields_/test_autofield.py new file mode 100644 index 000000000..6c4345c4e --- /dev/null +++ b/tests/model_fields_/test_autofield.py @@ -0,0 +1,9 @@ +from django.test import SimpleTestCase + +from django_mongodb.fields import ObjectIdAutoField + + +class MethodTests(SimpleTestCase): + def test_to_python(self): + f = ObjectIdAutoField() + self.assertEqual(f.to_python("1"), 1)