0

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.enter image description here

Snorki
  • 19
  • 3
  • Does this answer your question? [Need guidance with FilteredSelectMultiple widget](https://stackoverflow.com/questions/57820652/need-guidance-with-filteredselectmultiple-widget) – jackotonye Apr 16 '23 at 20:11

1 Answers1

0

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.

  • Thanks for the widget, but it's not enough, found the solution in this thread [Guidance with FilteredSelectMultiple widget](https://stackoverflow.com/questions/57820652/need-guidance-with-filteredselectmultiple-widget) – Snorki Apr 14 '23 at 17:59