From 17a0a6667c3f77908f9427f6489e298c828133e2 Mon Sep 17 00:00:00 2001 From: Kevan Swanberg Date: Thu, 21 Jul 2016 13:08:57 -0400 Subject: [PATCH] Refs #26796 -- Fixed ManyToManyField's limit_choices_to warning without a through model. --- django/db/models/fields/related.py | 3 ++- tests/invalid_models_tests/test_relative_fields.py | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index e2313e08fd..a41e93b73f 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1228,7 +1228,8 @@ class ManyToManyField(RelatedField): id='fields.W341', ) ) - if self.remote_field.limit_choices_to and self.remote_field.through: + if (self.remote_field.limit_choices_to and self.remote_field.through and + not self.remote_field.through._meta.auto_created): warnings.append( checks.Warning( 'limit_choices_to has no effect on ManyToManyField ' diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py index f42d6a5365..01941fc58c 100644 --- a/tests/invalid_models_tests/test_relative_fields.py +++ b/tests/invalid_models_tests/test_relative_fields.py @@ -176,6 +176,15 @@ class RelativeFieldTests(SimpleTestCase): field = Model._meta.get_field('m2m') self.assertEqual(field.check(from_model=Model), []) + def test_many_to_many_with_limit_choices_auto_created_no_warning(self): + class Model(models.Model): + name = models.CharField(max_length=20) + + class ModelM2M(models.Model): + m2m = models.ManyToManyField(Model, limit_choices_to={'name': 'test_name'}) + + self.assertEqual(ModelM2M.check(), []) + def test_many_to_many_with_useless_options(self): class Model(models.Model): name = models.CharField(max_length=20)