BeginnerCoder

Lesson 20 — Custom manager

Name your common queries

A manager is the object behind Post.objects. You can add your own methods to it, so instead of repeating filter(pub=True) in ten views you write Post.objects.published() once.

class PostManager:
    def published(self):
        return [p for p in self.rows if p["pub"]]

Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.

Why it matters: one named query is one place to fix when the rule changes ("published and not deleted").

Want it another way?
Task

Add a drafts() method that returns unpublished posts, and print both lists of titles.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Django Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.