May 2018
Beginner to intermediate
526 pages
11h 57m
English
To filter a QuerySet, you can use the filter() method of the manager. For example, we can retrieve all posts published in the year 2017 using the following QuerySet:
Post.objects.filter(publish__year=2017)
You can also filter by multiple fields. For example, we can retrieve all posts published in 2017 by the author with the username admin:
Post.objects.filter(publish__year=2017, author__username='admin')
This equates to building the same QuerySet chaining multiple filters:
Post.objects.filter(publish__year=2017) \ .filter(author__username='admin')
Read now
Unlock full access