diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index cc791d55..67062317 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -32,8 +32,8 @@ jobs: - name: Checkout Django uses: actions/checkout@v4 with: - repository: 'mongodb-forks/django' - ref: 'mongodb-5.1.x' + repository: 'timgraham/django' + ref: 'objectid-no-int' path: 'django_repo' persist-credentials: false - name: Install system packages for Django's Python test dependencies diff --git a/django_mongodb_backend/fields/auto.py b/django_mongodb_backend/fields/auto.py index 5bf84e67..50f3b5b8 100644 --- a/django_mongodb_backend/fields/auto.py +++ b/django_mongodb_backend/fields/auto.py @@ -22,19 +22,11 @@ def deconstruct(self): return name, path, args, kwargs def get_prep_value(self, value): - if value is None: - return None - # Accept int for compatibility with Django's test suite which has many - # instances of manually assigned integer IDs, as well as for things - # like settings.SITE_ID which has a system check requiring an integer. - if isinstance(value, (ObjectId | int)): + if value is None or isinstance(value, ObjectId): return value try: return ObjectId(value) except errors.InvalidId as e: - # A manually assigned integer ID? - if isinstance(value, str) and value.isdigit(): - return int(value) raise ValueError(f"Field '{self.name}' expected an ObjectId but got {value!r}.") from e def get_internal_type(self): @@ -46,14 +38,11 @@ def to_python(self, value): try: return ObjectId(value) except errors.InvalidId: - try: - return int(value) - except ValueError: - raise exceptions.ValidationError( - self.error_messages["invalid"], - code="invalid", - params={"value": value}, - ) from None + 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_/test_autofield.py b/tests/model_fields_/test_autofield.py index 1ba2edc4..7d4b81ce 100644 --- a/tests/model_fields_/test_autofield.py +++ b/tests/model_fields_/test_autofield.py @@ -1,3 +1,4 @@ +from django.core.exceptions import ValidationError from django.test import SimpleTestCase from django_mongodb_backend.fields import ObjectIdAutoField @@ -17,4 +18,6 @@ def test_get_internal_type(self): def test_to_python(self): f = ObjectIdAutoField() - self.assertEqual(f.to_python("1"), 1) + msg = "“1” is not a valid Object Id." + with self.assertRaisesMessage(ValidationError, msg): + f.to_python("1")