This is an old revision of the document!


Models

Methods, tips, and tricks for working with Django Models


Query

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() 
posts = posts.filter(pub_date__month=12)
# queries are done lazily and won't be ran until necessary
 
print(posts) # now the query actually runs