[Django] ORM | Database | Use of Q in filter

Regarding the query interface of the database in Django, under normal circumstances, I will check this one of the rookie tutorial, which is more comprehensive, but only limited to basic operations: Django ORM - single table instance

But some more advanced/complex operations need to be accumulated or searched online.

objects.filter()

book_list = Book.objects.filter(title="C++", writer="Jack")

When using filter like this, all conditions in brackets are equivalent to being connected with and.

So what if you want to use or? Is there a corresponding code? The answer is yes.

from django.db.models import Q
book_list = Book.objects.filter(Q(title="C++") | Q(writer="Jack"))

Instructions for using Q on the official website: Complex lookups with Q objects

Guess you like

Origin blog.csdn.net/qq_42438771/article/details/119348383