Skip to content

Instantly share code, notes, and snippets.

@lsemel
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsemel/2b52ba807975900ce80a to your computer and use it in GitHub Desktop.
Save lsemel/2b52ba807975900ce80a to your computer and use it in GitHub Desktop.
Example of chainable django queryset methods (for Django 1.6 and lower)
from django.db import models
from django.db.models import query
class PersonQuerySet(query.QuerySet):
def some_query(self):
return self.filter(...)
def some_other_filter(self):
return self.filter(...)
class PersonManager(models.Manager):
def get_query_set(self):
return PersonQuerySet(self.model)
def __getattr__(self, name, *args):
if name.startswith("_"): # or at least "__"
raise AttributeError
return getattr(self.get_query_set(), name, *args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment