Search code examples
djangodjango-querysetdjango-filter

Django-filter with ModelChoiceFilter filter displaying {key:value}


I'm using ModelChoiceFilter and provided the queryset the following queryset in the filter.py:

filters.py

class PagosFilter(django_filters.FilterSet):
    semana = django_filters.ModelChoiceFilter(
        queryset=Pagos.objects.order_by('semana').distinct('semana').values('semana'))

    class Meta:
        model = Pagos
        fields = ['semana', ]

in the HTML I'm getting the filter presents the choices like this:

{'semana':'2020-W06'}
{'semana':'2020-W05'}
{'semana':'2020-W04'}

instead of:

2020-W06
2020-W05
2020-W04

How can I get the values like that?


Solution

  • My problem it is that in the queryset I was using 'values', this is what is working now for me:

    lass PagosFilter(django_filters.FilterSet):
    semana = django_filters.ModelChoiceFilter(field_name='semana',
                                              queryset=Pagos.objects.order_by('semana').distinct('semana'))
    
    class Meta:
        model = Pagos
        fields = ['semana', ]