Q in django code example

Example 1: django import Q

from django.db.models import Q

Example 2: q django

from django.db.models import Q

Q(question__startswith='Who') | Q(question__startswith='What')

Example 3: and in django query filter

Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008)

Example 4: objects.filter django

>>> Entry.objects.filter(blog_id=4)

Example 5: django filter values with OR operator

Blog.objects.filter(pk__in=[1, 4, 7])

Example 6: how to work with django ornm __in

# Get blogs entries with id 1, 4 and 7
>>> Blog.objects.filter(pk__in=[1,4,7])

# Get all blog entries with id > 14
>>> Blog.objects.filter(pk__gt=14)