Skip to content

make ObjectIdAutoField.to_python() accept integers as strings #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
migrations
model_fields
model_forms
model_formsets
model_inheritance_regress
mutually_referential
nested_foreign_keys
Expand Down
13 changes: 8 additions & 5 deletions django_mongodb/fields/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Empty file added tests/model_fields_/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions tests/model_fields_/test_autofield.py
Original file line number Diff line number Diff line change
@@ -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)