Django multi-select widget?

前端 未结 1 1026
夕颜
夕颜 2020-11-28 20:14

The Django admin site makes use of a really cool widget:

\"enter

How can I mak

相关标签:
1条回答
  • 2020-11-28 20:43

    From the docs:

    The Django Admin application defines a number of customized widgets for calendars, filtered selections, and so on. These widgets define media requirements, and the Django Admin uses the custom widgets in place of the Django defaults. The Admin templates will only include those media files that are required to render the widgets on any given page.

    If you like the widgets that the Django Admin application uses, feel free to use them in your own application! They’re all stored in django.contrib.admin.widgets.

    In this case, you want the FilteredSelectMultiple widget. To use it, apply the widget on a form field like so:

    my_field = forms.ModelMultipleChoiceField(queryset=MyModel.objects.all(), widget=FilteredSelectMultiple("verbose name", is_stacked=False))
    

    Make sure to include the forms media in the template as it needs to include a few JS files.

    0 讨论(0)
提交回复
热议问题