Methods, tips, and tricks for working with Django Models
from ..models import Post # all posts posts = Post.objects.all() # filtered by field posts = Post.objects.filter(pub_date__year=2020) # can chain additional .filter() # queries are done lazily and won't be ran until necessary posts = posts.filter(pub_date__month=12) print(posts) # now the query actually runs
__ filters for queries
# use a __ (dunder) after a field name to access different forms of it ie posts = Post.objects.filter(title="A New Title") posts = Post.objects.filter(title__contains="New") # can also use icontains for case insensitive posts = Post.objects.filter(title__startswith="A") posts = Post.objects.filter(pub_date__year=2020) # can also use month, day