Fixed #26796 -- Added a system check for m2m fields with ignored limit_choices_to.

This commit is contained in:
Anderson Resende 2016-07-08 20:37:40 -03:00 committed by Tim Graham
parent 8a4f017f45
commit ba53da894f
3 changed files with 32 additions and 6 deletions

View File

@ -1228,6 +1228,15 @@ class ManyToManyField(RelatedField):
id='fields.W341', id='fields.W341',
) )
) )
if self.remote_field.limit_choices_to and self.remote_field.through:
warnings.append(
checks.Warning(
'limit_choices_to has no effect on ManyToManyField '
'with a through model.',
obj=self,
id='fields.W343',
)
)
return warnings return warnings

View File

@ -251,6 +251,8 @@ Related Fields
* **fields.W341**: ``ManyToManyField`` does not support ``validators``. * **fields.W341**: ``ManyToManyField`` does not support ``validators``.
* **fields.W342**: Setting ``unique=True`` on a ``ForeignKey`` has the same * **fields.W342**: Setting ``unique=True`` on a ``ForeignKey`` has the same
effect as using a ``OneToOneField``. effect as using a ``OneToOneField``.
* **fields.W343**: ``limit_choices_to`` has no effect on ``ManyToManyField``
with a ``through`` model.
Signals Signals
~~~~~~~ ~~~~~~~

View File

@ -181,7 +181,18 @@ class RelativeFieldTests(SimpleTestCase):
name = models.CharField(max_length=20) name = models.CharField(max_length=20)
class ModelM2M(models.Model): class ModelM2M(models.Model):
m2m = models.ManyToManyField(Model, null=True, validators=['']) m2m = models.ManyToManyField(
Model,
null=True,
validators=[''],
limit_choices_to={'name': 'test_name'},
through='ThroughModel',
through_fields=('modelm2m', 'model'),
)
class ThroughModel(models.Model):
model = models.ForeignKey('Model', models.CASCADE)
modelm2m = models.ForeignKey('ModelM2M', models.CASCADE)
errors = ModelM2M.check() errors = ModelM2M.check()
field = ModelM2M._meta.get_field('m2m') field = ModelM2M._meta.get_field('m2m')
@ -191,15 +202,19 @@ class RelativeFieldTests(SimpleTestCase):
'null has no effect on ManyToManyField.', 'null has no effect on ManyToManyField.',
obj=field, obj=field,
id='fields.W340', id='fields.W340',
) ),
]
expected.append(
DjangoWarning( DjangoWarning(
'ManyToManyField does not support validators.', 'ManyToManyField does not support validators.',
obj=field, obj=field,
id='fields.W341', id='fields.W341',
) ),
) DjangoWarning(
'limit_choices_to has no effect on ManyToManyField '
'with a through model.',
obj=field,
id='fields.W343',
),
]
self.assertEqual(errors, expected) self.assertEqual(errors, expected)