mirror of https://github.com/django/django.git
Fixed #26796 -- Added a system check for m2m fields with ignored limit_choices_to.
This commit is contained in:
parent
8a4f017f45
commit
ba53da894f
|
@ -1228,6 +1228,15 @@ class ManyToManyField(RelatedField):
|
|||
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
|
||||
|
||||
|
|
|
@ -251,6 +251,8 @@ Related Fields
|
|||
* **fields.W341**: ``ManyToManyField`` does not support ``validators``.
|
||||
* **fields.W342**: Setting ``unique=True`` on a ``ForeignKey`` has the same
|
||||
effect as using a ``OneToOneField``.
|
||||
* **fields.W343**: ``limit_choices_to`` has no effect on ``ManyToManyField``
|
||||
with a ``through`` model.
|
||||
|
||||
Signals
|
||||
~~~~~~~
|
||||
|
|
|
@ -181,7 +181,18 @@ class RelativeFieldTests(SimpleTestCase):
|
|||
name = models.CharField(max_length=20)
|
||||
|
||||
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()
|
||||
field = ModelM2M._meta.get_field('m2m')
|
||||
|
@ -191,15 +202,19 @@ class RelativeFieldTests(SimpleTestCase):
|
|||
'null has no effect on ManyToManyField.',
|
||||
obj=field,
|
||||
id='fields.W340',
|
||||
)
|
||||
]
|
||||
expected.append(
|
||||
),
|
||||
DjangoWarning(
|
||||
'ManyToManyField does not support validators.',
|
||||
obj=field,
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue