D
D
DmSS19972021-09-30 11:41:56
Django
DmSS1997, 2021-09-30 11:41:56

Native method for django-filter for MultipleChoiceFilter doesn't work. What is the reason and how to fix it?

I wrote my own ChoiceFilter for one field with special filtering conditions. But there was a need to change from the usual ChoiceFilter to MultipleChoiceFilter and the condition stopped working and I see no reason for this.

Filter code:

class DailyOrdersFilter(django_filters.FilterSet):
       STATUS_CHOICES3 = (
                (24, 'От 1 до 24'),
                (49, 'От 25 до 49'),
                (99, 'От 50 до 99'),
                (149, 'От 100 до 149'),
                (199, 'От 150 до 199'),
                (299, 'От 200 до 299'),
                (399, 'От 300 до 399'),
                (499, 'От 400 до 499'),
                (999, 'От 500 до 999'),
                (1000, 'От 1000 '),
                ('', 'Любая'),
        )
        square_for_filter = django_filters.MultipleChoiceFilter(choices=STATUS_CHOICES3, label='Требуемая площадь (в кв.м.)', method='square_filter')
        
            def square_filter(self, queryset, name, value):
        if value == '24':
            return queryset.filter(Q(square__icontains='Любой') | Q(square_for_filter__lte=value))
        if value == '49':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=25) & Q(square_for_filter__lte=value))
        if value == '99':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=50) & Q(square_for_filter__lte=value))
        if value == '149':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=100) & Q(square_for_filter__lte=value))
        if value == '199':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=150) & Q(square_for_filter__lte=value))
        if value == '299':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=200) & Q(square_for_filter__lte=value))
        if value == '399':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=300) & Q(square_for_filter__lte=value))
        if value == '499':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=400) & Q(square_for_filter__lte=value))
        if value == '999':
            return queryset.filter(
                Q(square__icontains='Любой') | Q(square_for_filter__gte=500) & Q(square_for_filter__lte=value))
        if value == '1000':
            return queryset.filter(Q(square__icontains='Любой') | Q(square_for_filter__gte=1000))


The model has two area attributes, one is a text attribute for filling the table in the template, and the second is a numeric field for filtering. Square and SquareForFilter respectively. "Any" in the condition is necessary so that when choosing an area from 1 to 25 meters, records for which the values ​​\u200b\u200bof "any" are also indicated in the table. For just ChoiceFilter everything worked.
The following error is thrown:
61557a36c2d10644985771.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question