24.3 Custom Managers and Querysets
A manager is any Python class that inherits from the Manager
class defined in Django’s db.models
package. Django automatically generates a manager for a model class unless the objects
attribute is explicitly declared.
To create and connect our custom PostManager
, we simply declare it as a subclass of Manager
and then add it as the objects
attribute on Post
. In Example 24.1, we also add the published()
method to find all Post
objects with a pub_date
from before today’s date, much like the published()
method defined in Tag
and Startup
.
1 from datetime import date . ... 14 class PostManager(models.Manager): 15 16 ...
Get Django Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.