Django Q and reduce работает не так, как ожидалось

Я недавно в Django. Не понимаю, где я ошибаюсь. Когда я использую этот код, он работает так, как ожидается.

def filter_qs(self, qs):
    if self.filter == {}:
        return qs
    else:
        q_objects = [models.Q()]
        if self.filter['vendor']:
            q_objects.append(Q(vendor__in=self.filter['vendor']))
        if self.filter['start_date']:
            q_objects.append(Q(date__gte=self.filter['start_date']))
        if self.filter['end_date']:
            q_objects.append(Q(date__lte=self.filter['end_date']))
        if self.filter['product']:
            product_list = (Q(product_type__contains=product) for product in self.filter['product'])
            if product_list:
                q_objects.append(reduce(operator.or_, product_list))

        if len(q_objects) > 1:
            qs = qs.filter(reduce(operator.and_, q_objects))

Однако, когда я использую фильтр запроса,

filter_query = Result.objects.filter(
    Q(vendor__in=vendor) if vendor
    else Q() & Q(date__gte=start_date) if start_date
    else Q() & Q(date__lte=end_date) if end_date
    else Q() & Q(reduce(operator.or_, (Q(product_type__contains=product) for product in products))))

эта часть запроса не работает в filter_query:

Q(reduce(operator.or_, (Q(product_type__contains=product) for product in products)))

У меня не работает фильтр запросов по каждому товару. Не понимаю, где я ошибаюсь

Посмотрите, чтобы запрос работал правильно.

Вернуться на верх