Stop writing Django querysets.
Project description
A Django utility that makes it simple to write DRY queryset methods.
Usage
Create a manager class that inherit the QuerySetManager
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):
@queryset_method
def by_slug(queryset, slug):
return queryset.filter(slug=slug)
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)
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)
Mix-in
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):
@queryset_method
def by_slug(queryset, slug):
return queryset.filter(slug=slug)
Planned Obsolescence
This project will be partially obsoleted by Django 1.7, when you can do this:
from django.db.models import Manager
from django.db.models.query import QuerySet
class MyQuerySet(QuerySet):
def by_slug(self, slug):
return queryset.filter(slug=slug)
MyManager = Manager.from_queryset(MyQuerySet)
However, if you still need to extend existing managers, or you don’t want to worry about the upgrade path from Manager.get_query_set to Manager.get_queryset in your own code, you may want to stick with this package.
Installation
$ pip install dj-queryset-manager
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for dj-queryset-manager-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9e68ee3a337fa7563e86cc80f1eaaf7e3769033a384d5235672e1a734625cec |
|
MD5 | c16601193da29b8e9b48ac0b94b989a0 |
|
BLAKE2b-256 | 9e18823858a463cc153e1b67e9d7ae4c306737042049098f15375655406a467c |