How to achieve the display of the field for the form (when it is filled) as in the admin panel (screenshot)?
I tried using forms.CheckboxSelectMultiple
widget but it doesn't quite fit. It provides the entire list from the request as a whole, this is not very convenient, since my list will be very large. We need a slider and search fields, as it is actually implemented in the admin panel.
To achieve the same display for a field in a form as in the admin panel, you can use Django's FilteredSelectMultiple
widget.
This widget includes a search field and a slider to make selecting multiple items from a large list easier.
You can import this widget from django.contrib.admin.widgets and use it in your form field like this:
from django.contrib.admin.widgets import FilteredSelectMultiple
from django.forms import ModelMultipleChoiceField, ModelForm
class MyForm(ModelForm):
my_field = ModelMultipleChoiceField(
queryset=MyModel.objects.all(),
widget=FilteredSelectMultiple("verbose name", is_stacked=False)
)
Make sure to replace MyModel
with the name of your model and adjust the widget's verbose name and is_stacked parameter to your needs.