[Fixed]-Django โ€“ filter objects (multiple categories of the same model)

1๐Ÿ‘

โœ…

Is it possible to write something like??:

   sites = Site.objects.filter(category=category or category1=category, is_active=True)

Yes, use the Q object when you want to use or:

If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.

A Q object (django.db.models.Q) is an object used to encapsulate a collection of keyword arguments. These keyword arguments are specified as in โ€œField lookupsโ€ above.

from django.db.models import Q
sites = Site.objects.filter(
    Q(category=category) | Q(category1=category), 
    is_active=True)

Leave a comment