Search code examples
djangodjango-rest-frameworkdjango-formsdjango-templatesdjango-filter

Django_filters Widget Customize


How Can I Customize Widgets in Django_filters? I tried To Did Like This But There its make Value in Select Field GoneImage When Adding Widget, and When i removed the widget the value is shown Image When Remove Widget, Sorry for my bad English, Thanks Before

class CustomerOrderFilter(django_filters.FilterSet):
    product = django_filters.ChoiceFilter(
        widget=forms.Select(attrs={'class': 'form-control'}))
    status = django_filters.ChoiceFilter(
        widget=forms.Select(attrs={'class': 'form-control'}))

    class Meta:
        model = Order
        fields = '__all__'
        exclude = ('customer', 'date_created', 'updated',)

Solution

  • ChoiceField is generic, you need to explicitly provide choices in your product field

    class CustomerOrderFilter(django_filters.FilterSet):
        product = django_filters.ChoiceFilter( 
            # replace choices with the choices defined in your order model or just copy it over
            choices=<PRODUCT_CHOICES>,
            widget=forms.Select(attrs={'class': 'form-control'}))