How to return a list of selected by user values using ArrayAgg in Django?

I am trying to write a query where I get the list of all matched filters per product.

I managed to write an annotation that creates a list and puts all matching filters:

def filter_data(request):
    client_type = request.GET.getlist('client_type[]')
    product_list = product_list.annotate(client_type_product_count=Count('client_type', filter=Q(client_type__title__in=client_type)), client_type_title=ArrayAgg('client_type__title', distinct=True)).exclude(client_type_product_count__exact=0)

The only thing I want to change is that in this array is that I will only see matched filters that the user selected. Right now I can see all of them (no matter what user selects I always see all matching filters per product.

For instance, I have a product that matches client_type1, client_type2 and client_type3 filters. Right now it shows me all 3 matches in a list. What I want is if the user selects client_type1 the list will only include client_type1 in a list (not all of them). If user selects client_type1 and client_type2 then it will show me only these 2 matched filters etc.

How can I do something like that?

from django.contrib.auth.models import User users = User.objects.all()

return Response( users.values_list('username', flat=True) )

OR

return Response( '\n'.join([user.username for user in users]) )

Back to Top