From 4bd90c770640fcfefb535e55a5fadabbcc32d80e Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 25 Oct 2024 19:42:06 -0400 Subject: [PATCH 1/2] add model_formsets tests to CI --- .github/workflows/test-python.yml | 1 + django_mongodb/features.py | 2 ++ 2 files changed, 3 insertions(+) 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/features.py b/django_mongodb/features.py index b17f3abe7..bbc141153 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -82,6 +82,8 @@ class DatabaseFeatures(BaseDatabaseFeatures): # https://github.com/mongodb-labs/django-mongodb/issues/161 "queries.tests.RelatedLookupTypeTests.test_values_queryset_lookup", "queries.tests.ValuesSubqueryTests.test_values_in_subquery", + # ObjectIdAutoField.to_python() doesn't accept integers as strings. + "model_formsets.tests.ModelFormsetTest.test_inline_formsets_with_custom_save_method", } # $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3. _django_test_expected_failures_bitwise = { From 8bb17eedb49dfa4cd8b057428e52f2fad7751575 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Fri, 25 Oct 2024 19:42:32 -0400 Subject: [PATCH 2/2] make ObjectIdAutoField.to_python() accept integers as strings --- django_mongodb/features.py | 2 -- django_mongodb/fields/auto.py | 13 ++++++++----- tests/model_fields_/__init__.py | 0 tests/model_fields_/test_autofield.py | 9 +++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 tests/model_fields_/__init__.py create mode 100644 tests/model_fields_/test_autofield.py diff --git a/django_mongodb/features.py b/django_mongodb/features.py index bbc141153..b17f3abe7 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -82,8 +82,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): # https://github.com/mongodb-labs/django-mongodb/issues/161 "queries.tests.RelatedLookupTypeTests.test_values_queryset_lookup", "queries.tests.ValuesSubqueryTests.test_values_in_subquery", - # ObjectIdAutoField.to_python() doesn't accept integers as strings. - "model_formsets.tests.ModelFormsetTest.test_inline_formsets_with_custom_save_method", } # $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3. _django_test_expected_failures_bitwise = { 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)