From 460a13692b09b623e14c014d3aef8108e714e6ac Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 28 Oct 2024 09:19:31 -0400 Subject: [PATCH 1/2] add test for ObjectIdAutoField.deconstruct() --- tests/model_fields_/test_autofield.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/model_fields_/test_autofield.py b/tests/model_fields_/test_autofield.py index 6c4345c4e..83012cab2 100644 --- a/tests/model_fields_/test_autofield.py +++ b/tests/model_fields_/test_autofield.py @@ -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.auto.ObjectIdAutoField") + self.assertEqual(args, []) + self.assertEqual(kwargs, {"db_column": "_id", "primary_key": True}) + def test_to_python(self): f = ObjectIdAutoField() self.assertEqual(f.to_python("1"), 1) From a06efe3582f008c3fdb2f944b189b0653da45666 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 28 Oct 2024 09:23:24 -0400 Subject: [PATCH 2/2] implement ObjectIdAutoField.deconstruct() Remove "auto" from path and omit default db_column from kwargs. --- django_mongodb/fields/auto.py | 8 ++++++++ tests/model_fields_/test_autofield.py | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django_mongodb/fields/auto.py b/django_mongodb/fields/auto.py index 229386126..7509d4b42 100644 --- a/django_mongodb/fields/auto.py +++ b/django_mongodb/fields/auto.py @@ -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") + return name, path, args, kwargs + def get_prep_value(self, value): if value is None: return None diff --git a/tests/model_fields_/test_autofield.py b/tests/model_fields_/test_autofield.py index 83012cab2..0013b2794 100644 --- a/tests/model_fields_/test_autofield.py +++ b/tests/model_fields_/test_autofield.py @@ -7,9 +7,9 @@ class MethodTests(SimpleTestCase): def test_deconstruct(self): field = ObjectIdAutoField() name, path, args, kwargs = field.deconstruct() - self.assertEqual(path, "django_mongodb.fields.auto.ObjectIdAutoField") + self.assertEqual(path, "django_mongodb.fields.ObjectIdAutoField") self.assertEqual(args, []) - self.assertEqual(kwargs, {"db_column": "_id", "primary_key": True}) + self.assertEqual(kwargs, {"primary_key": True}) def test_to_python(self): f = ObjectIdAutoField()