I want to filter ManyToManyField options in my ModelForm:
class MyForm(forms.ModelForm):
class Meta:
model = Entity
fields = ['parent_entities']
def __init__(self, *args, **kwargs):
self.root_entity = kwargs.pop('root_entity')
self.Meta.fields['parent_entities'].queryset = Entity.objects.filter(root_entity=self.root_entity)
super(MyForm, self).__init__(*args, **kwargs)
I tried a lot of other code that I saw, but nothing worked.
I think my problem is that I cannot get this parent_entities field. With this code, I have an error:
list indices must be integers, not str
source
share