I’m new to Django and am working through a variety of tutorials. I came across this model manager. Which is only used once in the tutorial to list published posts as opposed to draft.
Why would I use that instead of the following, which seems to me much simpler?
One of the benefits of a manager is that it can provide an abstraction layer, that may make other parts of your code easier to understand.
So let’s say you have a section of your project that only works with published posts. You can either make sure you have that filter on every query, or you build a manager that does that for you.
For one such query, sure - you probably wouldn’t worry about a manager in that situation.
However, I’m working on a project where we have, for one table, 58 queries on that table, with many of the queries having multiple filters. Rather than trying to ensure that all the conditions are applied correctly to each instance, we have managers (yes, plural) built to categories those queries as a “base queryset” regardless of the view or utility module in which it’s being used.