In my viewSet I am doing a query,
queryset= Books.objects.all();
Now from an ajax call I get my filter values from UI i.e. age,gender, etc. of
You haven't shown any code, so you haven't really explained what the problem is:
Start with the queryset Book.objects.all()
. For each filter, check if there is a value for the filter in request.POST
, and if so, filter the queryset. Django querysets are lazy, so only the final queryset will be evaluated.
queryset = Book.objects.all()
if request.POST.get('age'):
queryset = queryset.filter(author__age=request.POST['age'])
if request.POST.get('gender'):
queryset = queryset.filter(author__gender=request.POST['gender'])
...