Q Objects In Django ?✅

 Q Object is an object used to encapsulate a collection of keyword arguments. These keyword arguments are specified in as "Field Lookups"

If you need to execute more complex queries, you can use Q objects.

Q Objects can be combined using the & and | Operators. When an operator is used on two Q Objects, it yields a new Q object.

from django.db.models import Q

& (AND) Operator

Example: Student.objects.filter(Q(id=6) & Q(id=106))

| (OR) Operator

Example: Student.objects.filter(Q(id=6) | Q(id=108))

~ (Negation) Operator

Example: Student.objects.filter(~Q(id=6) )

Post a Comment

0 Comments