Answer the question
In order to leave comments, you need to log in
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))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question