[Answered ]-Django: limit_choices_to with with a "through" intermediate table

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)) 
👤xyres

Leave a comment