Reputation: 5815
https://docs.djangoproject.com/en/1.10/ref/models/fields/:
"limit_choices_to has no effect when used on a ManyToManyField with a custom intermediate table specified using the through parameter."
Why?! And what to do if I need both through
and limit_choices_to
?
Should I fall back from ModelForm
to simple Form
in this situation and do it all manually? Or is there a way to do it with ModelForm
?
Upvotes: 1
Views: 297
Reputation: 21834
I tested limit_choices_to
with a through
ManyToManyField
. And surprisingly it works, despite the docs say it doesn't.
Still, if it's not working for you, you can set a custom queryset for the ManyToMany
field in your ModelForm
.
# models
class YourModel(models.Model):
some_attr = models.BooleanField()
class MyModel(models.Model):
my_field = models.ManyToManyField(YourModel, through=...)
# forms
class MyModelForm(forms.ModelForm):
...
my_field = forms.ModelMultipleChoiceField(queryset=YourModel.objects.filter(some_attr=True))
Upvotes: 2