Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Stop writing Django querysets.

Project description

A Django utility that makes it simple to write DRY queryset methods.


dj-queryset-manager only works with Django versions 1.2 through 1.6.

In Django 1.7 it has been superseded by QuerySet.as_manager(). See the docs.


  1. Create a manager class that inherit the QuerySetManager
  2. Decorate your filter methods with queryset_method. These methods receive a queryset instead of a manager as the first argument.
from dj_queryset_manager import QuerySetManager, queryset_method

class MyManager(QuerySetManager):
    def by_slug(queryset, slug):
        return queryset.filter(slug=slug)

    def filter(queryset, *args, **kwargs):
        return super(type(queryset), queryset).filter(*args, **kwargs)

For reference, here is a standard implementation:

from django.db.models import Manager
from django.db.models.query import QuerySet

class MyQuerySet(QuerySet):
    def by_slug(self, slug):
        return self.filter(slug=slug)

    def filter(self, *args, **kwargs):
        return super(MyQuerySet, self).filter(*args, **kwargs)

class MyManager(Manager):
    def get_query_set(self): # Better remember the arguments to QuerySet
        QuerySet(self.model, using=self._db)

    def get_queryset(self): # And don't forget about Django 1.6
        return self.get_query_set()

    def by_slug(self, *args, **kwargs):  # Enjoy this duplicate signature
        return self.get_queryset().filter(*args, **kwargs)


Some third-party apps ship managers as part of their API. If you need to extend any existing manager, use the QuerySetManagerMixin. This example uses the InheritanceManager from django-model-utils.

from model_utils.managers import InheritanceManager
from dj_queryset_manager import QuerySetManagerMixin, queryset_method

class MyInheritanceManager(QuerySetManagerMixin, InheritanceManager):
    def by_slug(queryset, slug):
        return queryset.filter(slug=slug)


$ pip install dj-queryset-manager

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for dj-queryset-manager, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size dj-queryset-manager-0.1.3.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page