Search code examples
djangodjango-filter

MultipleChoiceFilter to Checkboxes


I have a ModelMultipleChoiceFilter working, however I cant find a way to adjust it from a input multiple choice to a checkbox list.

This is what I currently have:

What I currently have

This is what I want to convert it to:

enter image description here

My current code:

class GameFilter(django_filters.FilterSet):

gamemodes = django_filters.ModelMultipleChoiceFilter(
        queryset=GameMode.objects.all(),
        label='Game modes (or)',
    )

Solution

  • Simply pass the keyword argument widget with CheckboxSelectMultiple [Django docs] to your filter:

    from django import forms
    
    class GameFilter(django_filters.FilterSet):
        gamemodes = django_filters.ModelMultipleChoiceFilter(
            queryset=GameMode.objects.all(),
            label='Game modes (or)',
            widget=forms.CheckboxSelectMultiple,
        )