When would you use a custom Manager versus a custom QuerySet?
Last updated
A custom
QuerySetholds reusable, chainable filters (e.g.published().by_author(x)) that work anywhere a queryset flows.A custom
Manageris the model's entry point: overrideget_queryset()to change the default set, or add non-chainable table-level helpers.Combine them with
as_manager()orManager.from_queryset()to get both without duplicating code.
- https://docs.djangoproject.com/en/6.1/topics/db/managers/
- https://python.plainenglish.io/enhancing-security-and-maintainability-custom-managers-and-querysets-in-django-638f77e69117
- https://fly.io/django-beats/organizing-database-queries-managers-vs-querysets/
- https://kimkani.tistory.com/67
- https://www.otovo.io/2023/08/17/default-managers-in-django/
- https://python.plainenglish.io/enhancing-custom-managers-with-querysets-in-django-2d9d36f8d0c4