View Post

Django Admin ManyToMany Add Button

In Django, Pythonby oesahLeave a Comment

Finally a working solution: # forms.py from django.contrib.admin.widgets import FilteredSelectMultiple, RelatedFieldWidgetWrapper class MyForm(forms.ModelForm): m2m_field = forms.ModelMultipleChoiceField(queryset=None,label=(‚Select Entities‘),) def __init__(self, *args, **kwargs): super(MyForm,self).__init__(*args, **kwargs) self.fields[‚m2m_field‘].widget = RelatedFieldWidgetWrapper( FilteredSelectMultiple((‚m2m_field‘),False,), MyModel._meta.get_field(‚m2m_field‘).rel, self.admin_site) self.fields[‚m2m_field‘].queryset …