Filtering, Getting and Chaining
Managers and querysets. Post.objects is the manager. Calling something on it returns a queryset, and querysets can be chained: Post.objects.filter(published=True).order_by("-created")[:10].
filter and get are not the same shape.
filter() returns a queryset, possibly empty. Asking for something that is not there is a normal outcome.
get() returns exactly one object, and raises otherwise: DoesNotExist when there is nothing, MultipleObjectsReturned when there is more than one.
In a view, use get_object_or_404. It is get() with the right behavior for the web already attached.
Lookups use a double underscore. title__icontains="sankofa", created__year=2026, views__gte=100, author__in=[a, b], slug__isnull=True.
It also follows relationships: Post.objects.filter(author__username="amara") filters posts by a field on the related user, in one query.
Mark this lesson complete
Signed in, your progress follows you to every device.