2👍
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))
Source:stackexchange.com