[Answered ]-How to filtered 2 Q conditions in query django?

1👍

It is simpler to just check the list of brand_names and filter if it contains at least one element:

def some_brand_mobiles(*brand_names):
    if brand_names:
        return Mobile.objects.filter(brand__name__in=brand_names)
    else:
        return Mobile.objects.all()

0👍

Try this solution:

def some_brand_mobiles(*brand_names):
    queryset = Mobile.objects.all()
    if brand_names:
        queryset = queryset.filter(brand__name__in=brand_names)
    return queryset

In that way you can add more filters based on any other condition over the queryset.

Leave a comment