Home > Enterprise >  Which field is this Django field and how to use it?
Which field is this Django field and how to use it?

Time:10-28

This can be found in Django Admin, under Groups table.

Picture: https://imgur.com/a/Of9ZASM

As I see, it is a <select> html tag with the multiple option (<select multiple>). How can we achieve it in custom tables, and how can we handle them?

I looked up the django documentation, but it's not that documented (if I found the right one).

CodePudding user response:

You can work with a FilteredSelectMultiple widget. You can import this widget from the admin widgets. This will also require CSS and JavaScript. In your form you thus can work with:

from django.contrib.admin.widgets import FilteredSelectMultiple

class SomeModelForm(forms.ModelForm):
    class Meta:
        model = SomeModel
        widgets = {
            'some_field': FilteredSelectMultiple('SomeField', False)
        }
    
    class Media:
        css = {
            'all': ('/static/admin/css/widgets.css',),
        }
        js = ('/admin/jsi18n',)

You then render the form with:

<form method="post" action="…">
    {% csrf_token %}
    {{ form.media }}
    {{ form }}
</form>
  • Related