D
D
DmSS19972021-07-03 12:09:21
Django
DmSS1997, 2021-07-03 12:09:21

How to implement multi-parameter search in django-filter?

Implemented filtering by table fields.
60e02776d989c295022945.png
60e0279c83f2c233438507.png

The filtering works as it should, but I need that when selecting, say, the first floor, along with the first floor, the table still shows records where the floor still has a mark of any, i.e. you select the first floor in the filter and the records are filtered so that 1- 1st floor and any floor. I don't understand how this can be implemented. Please help me understand
Here is the code from filters.py

class DailyOrdersFilter(django_filters.FilterSet):
    STATUS_CHOICES1 = (
        ('-1', '-1'),
        ('1', '1'),
        ('2', '2'),
        ('3', '3'),
        ('4', '4'),
        ('', 'любой'),
    )
    floor = django_filters.ChoiceFilter(choices=STATUS_CHOICES1, label='Требуемый этаж', lookup_expr='icontains')

    class Meta:
        model = DailyOrders
        fields = ['floor']


I assume that there is no ready-made solution and you need to write the method yourself, but I don’t understand how to do it. Please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DmSS1997, 2021-07-03
@DmSS1997

I managed to write it myself.
Method looks simple enough, as it turned out:

def floor_filter(self,queryset, name, value):
        return queryset.filter(Q(floor__icontains='любой')|Q(floor__icontains=value))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question