Categories
Python Answers

How to do an OR filter in a Python Django query?

Spread the love

To do an OR filter in a Python Django query, we can use Q and the | operator.

For instance, we write

from django.db.models import Q

Item.objects.filter(Q(creator=owner) | Q(moderated=False))

to call filter to search for Item items where creator is owner or moderated is False.

We call Q with the conditions we want and combine them with | to do the OR query.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *