diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/field_deconstruction/tests.py | 28 | ||||
| -rw-r--r-- | tests/schema/fields.py | 7 |
2 files changed, 34 insertions, 1 deletions
diff --git a/tests/field_deconstruction/tests.py b/tests/field_deconstruction/tests.py index b746e46458..846f6d3ed7 100644 --- a/tests/field_deconstruction/tests.py +++ b/tests/field_deconstruction/tests.py @@ -432,6 +432,34 @@ class FieldDeconstructionTests(SimpleTestCase): self.assertEqual(kwargs, {"to": "auth.Permission"}) self.assertEqual(kwargs['to'].setting_name, "AUTH_USER_MODEL") + def test_many_to_many_field_related_name(self): + class MyModel(models.Model): + flag = models.BooleanField(default=True) + m2m = models.ManyToManyField('self') + m2m_related_name = models.ManyToManyField( + 'self', + related_name='custom_name', + related_query_name='custom_query_name', + limit_choices_to={'flag': True}, + ) + + name, path, args, kwargs = MyModel.m2m.field.deconstruct() + self.assertEqual(path, 'django.db.models.ManyToManyField') + self.assertEqual(args, []) + # deconstruct() should not include attributes which were not passed to + # the field during initialization. + self.assertEqual(kwargs, {'to': 'field_deconstruction.MyModel'}) + # Passed attributes. + name, path, args, kwargs = MyModel.m2m_related_name.field.deconstruct() + self.assertEqual(path, 'django.db.models.ManyToManyField') + self.assertEqual(args, []) + self.assertEqual(kwargs, { + 'to': 'field_deconstruction.MyModel', + 'related_name': 'custom_name', + 'related_query_name': 'custom_query_name', + 'limit_choices_to': {'flag': True}, + }) + def test_positive_integer_field(self): field = models.PositiveIntegerField() name, path, args, kwargs = field.deconstruct() diff --git a/tests/schema/fields.py b/tests/schema/fields.py index e4b62eab39..aaba202364 100644 --- a/tests/schema/fields.py +++ b/tests/schema/fields.py @@ -34,7 +34,12 @@ class CustomManyToManyField(RelatedField): self.db_table = db_table if kwargs['rel'].through is not None: assert self.db_table is None, "Cannot specify a db_table if an intermediary model is used." - super().__init__(**kwargs) + super().__init__( + related_name=related_name, + related_query_name=related_query_name, + limit_choices_to=limit_choices_to, + **kwargs, + ) def contribute_to_class(self, cls, name, **kwargs): if self.remote_field.symmetrical and ( |
