Mighty Model Managers

Shawn Inman

Tuesday 4:40 p.m.–5:10 p.m.
Audience level: Intermediate or Advanced


Model Managers are an amazing part of the Django Framework. When put to use, they can bring enhanced readability, encapsulation of logic, increased security, and performance. But they're often overlooked - even by those with years of experience. Let's fix that. We'll go through examples that demonstrate how easy Model Managers are to integrate into a project and why they're so important.


Intro (1m)

  • Quick info about me and why I'm presenting on this topic.

What Are Model Managers? (3m)

  • Dispel the idea that anyone isn't using MM. They're built in, even if people don't know it
  • Review what is provided for you out of the box
  • Explain the abstraction from the ORM

Talk about our sample project (2m)

  • Introduce a sample project (perhaps the sample project from the Django docs) that we'll work through improving using MMs

Override the default Manager (3m)

  • Basic setup
  • QuerySet.as_manager() (more for free!)

Readability and Reuse (3m)

Readability is an easy win from using Model Managers and is a good first step toward understanding their benefits.

  • Demonstrate chaning (composability) of Model Manager methods to improve readability
  • DRY! - using Model Manager methods simplifies the repetition of writing the same queries across the codebase

Encapsulation of logic (5m)

Encapsulation is a major difference-maker for apps with significant business logic.

  • Provide a consistent interface, even when the app evolves over time
  • Effortlessly enable soft-deletes, etc.
  • Preventing data leakage: protect future-you from present-you! (show an example of how much easier it is to use a Model Manager method than remembering to exclude records based on a status, etc)

Cacheability (3m)

  • "Russian Doll" caching and applying the idea to queries
  • Composeability of model manager methods allows you to selectively cache the results from Model Manager methods, particularly if non-query operations are going on there.

Q/A (5m)

Buy your conference and tutorial tickets here! Tutorials are $150 per session.