[Django]-How to create multiple filters in Django?

1👍

A simple solution to your problem is to create complex queries with Q objects.
You can find more information on that matter here.

Otherwise you can try multiple filters at once:

varName = Room.objects.filter(category__in=categories) | Room.objects.filter(capacity__in=capacities) | Room.objects.filter(capacity__in=capacities)...

use the | symbol to separate the queries

👤haduki

1👍

To query multiple things at the same time, use the | symbol to separate them

varName = Room.objects.filter(category__in=categories) | Room.objects.filter(capacity__in=capacities) | Room.objects.filter(capacity__in=capacities)...

I recently used this in a search view like this

object_list = Post.objects.filter(title__icontains=query) | Post.objects.filter(content__icontains=query) | Post.objects.filter(category__icontains=query) | Post.objects.filter(date_posted__icontains=query) | Post.objects.filter(author__username__icontains=query)
👤JDODER

Leave a comment