PyNotes
250 subscribers
125 photos
7 videos
1 file
61 links
**Code is communication**
admin: @Xojarbu
https://t.iss.one/solutions_py for problem solutions
Download Telegram
#Django_ORM #filters
OR in filters:
1) queryset = User.objects.filter( first_name__startswith='R') | User.objects.filter(last_name__startswith='D')

2)qs = User.objects.filter(Q(first_name__startswith='R')|Q(last_name__startswith='D'))

In both cases SQL query will be the same
#Django_ORM #filters
AND in filters:
1) queryset = User.objects.filter( first_name__startswith='R') and User.objects.filter(last_name__startswith='D')
2) queryset_1 & queryset_2
3)filter(Q(<condition_1>) & Q(<condition_2>))
#Django_ORM #filters
NOT
1)queryset = User.objects.filter(~Q(id__lt=5))
2)exclude(condition)
#Django_ORM #filters
UNION
q1.union(q2)
#Django_ORM #filters
Subquery

Django allows using SQL subqueries. Let’s start with something simple, We have a UserParent model which has OnetoOne relation with auth user. We will find all the UserParent which have a UserParent.

from
django.db.models import Subquery
users = User.objects.all()
UserParent.objects.filter(user_id__in=Subquery(users.values('id')))

#source
https://books.agiliq.com/projects/django-orm-cookbook/en/latest/subquery.html
👍1
#Django_ORM #filters
F
F
is used to filter fields of the same model:
User.objects.filter(last_name=F("first_name"))