Managers & Custom QuerySets
Managers are the interface through which database query operations are provided to Django models. The default manager is 'objects'. You can create custom managers and QuerySets to encapsulate common query patterns and keep your views clean.
15 min•By Priygop Team•Updated 2026
Custom Managers
- Default manager: Model.objects (instance of Manager)
- Custom managers add reusable query methods
- Custom QuerySets allow chainable custom methods
- Use as_manager() to convert QuerySet to Manager
- Override get_queryset() to change default behavior
- Multiple managers per model are supported
Custom Manager Example
Custom Manager Example
# blog/models.py
# from django.db import models
# Custom QuerySet with chainable methods
# class PostQuerySet(models.QuerySet):
# def published(self):
# return self.filter(published=True)
#
# def by_author(self, user):
# return self.filter(author=user)
#
# def recent(self, count=10):
# return self.order_by('-created_at')[:count]
#
# def popular(self):
# return self.order_by('-views_count')
# Custom Manager
# class PostManager(models.Manager):
# def get_queryset(self):
# return PostQuerySet(self.model, using=self._db)
#
# def published(self):
# return self.get_queryset().published()
# class Post(models.Model):
# title = models.CharField(max_length=200)
# published = models.BooleanField(default=False)
# views_count = models.IntegerField(default=0)
#
# # Custom manager
# objects = PostManager()
#
# Usage — clean, chainable queries:
# Post.objects.published() # All published posts
# Post.objects.published().popular() # Published + popular
# Post.objects.published().by_author(user).recent(5)Tip
Tip
Use the Django shell (python manage.py shell) to test ORM queries interactively before putting them in views.
Diagram
Loading diagram…
QuerySets are LAZY — no DB hit until evaluated.
Common Mistake
Warning
Doing database queries inside templates. Keep business logic in views or model methods, not templates.
Practice Task
Note
(1) Build a complete blog data model. (2) Test all CRUD operations in shell. (3) Use aggregation for post statistics.
Quick Quiz
Key Takeaways
- Managers are the interface through which database query operations are provided to Django models.
- Default manager: Model.objects (instance of Manager)
- Custom managers add reusable query methods
- Custom QuerySets allow chainable custom methods