Updated at Aug 17, 2016:
I've updated the article mentioning the DocumentQuerySet.as_manager() option. Thanks to
Darryl and Mark for suggesting in the comments below!
In a Django model, the Manager is the interface that interacts with the database. By default the manager is
available through the Model.objects property. The default manager every Django model gets out of the box is the
django.db.models.Manager. It is very straightforward to extend it and change the default manager.
With that you will be able to retrieve all pdf files like this:
The thing is, this method is not chainable. I mean, you can still use order_by or filter in the result:
But if you try to chain the methods it will break:
To make it work you must create custom QuerySet methods:
Now you can use it just like any other QuerySet method:
If you are only defining custom QuerySets in the Manager, you can simply extend the models.QuerySet and in the model
set the manager as objects = DocumentQuerySet.as_manager():
You can keep the code inside the models.py. But as the code base grow, I prefer to keep the Managers and QuerySets
in a different module, named managers.py.