Skip to content

implement ObjectIdAutoField.deconstruct() #177

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
8 changes: 8 additions & 0 deletions django_mongodb/fields/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ def __init__(self, *args, **kwargs):
kwargs["db_column"] = "_id"
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
if self.db_column == "_id":
del kwargs["db_column"]
if path.startswith("django_mongodb.fields.auto"):
path = path.replace("django_mongodb.fields.auto", "django_mongodb.fields")
Comment on lines +22 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we do this? Just for consistency in path imports or is there a technical reason, too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to https://github.com/django/django/blob/163e72ebbaa84804877f3d1ae212575e479b533b/django/db/models/fields/__init__.py#L646-L657, the fact that our fields are implemented in submodules of django_mongodb.fields is an implementation detail that's fine to hide from end users.

return name, path, args, kwargs

def get_prep_value(self, value):
if value is None:
return None
Expand Down
7 changes: 7 additions & 0 deletions tests/model_fields_/test_autofield.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@


class MethodTests(SimpleTestCase):
def test_deconstruct(self):
field = ObjectIdAutoField()
name, path, args, kwargs = field.deconstruct()
self.assertEqual(path, "django_mongodb.fields.ObjectIdAutoField")
self.assertEqual(args, [])
self.assertEqual(kwargs, {"primary_key": True})

def test_to_python(self):
f = ObjectIdAutoField()
self.assertEqual(f.to_python("1"), 1)