Using django admin site's multiple choice field

Viewed 799

I guess most of you have paid attention to the admin site's multiple choice field:

enter image description here

I want the exact same widget to my MultipleChoiceField in my form. How is it possible to use that widget?

1 Answers

You can use the FilteredSelectMultiple, for example writing a field into admin form

from django import forms
from django.contrib.admin import widgets

class SomeAdminForm(forms.ModelForm):
    some_field = forms.ModelMultipleChoiceField(
        widget=widgets.FilteredSelectMultiple('Field name', False)
        queryset=SomeModel.objects.all()
    )

    class Meta:
        model = YourModel
        fields = ['your_model_fields', 'some_field']

It's works for forms.MultipleChoiceField.

The Django admin makes all the magic.

Related