Skip to content

Instantly share code, notes, and snippets.

@chrislawlor
Created March 13, 2013 13:57
Show Gist options
  • Save chrislawlor/5152331 to your computer and use it in GitHub Desktop.
Save chrislawlor/5152331 to your computer and use it in GitHub Desktop.
Chainable Model Manager boilerplate
import datetime
from django.db import models
from django.db.models.query import QuerySet
class PostQuerySet(QuerySet):
def live(self):
"""Filter out posts that aren't ready to be published"""
now = datetime.datetime.now()
return self.filter(date_published__lte=now, status="published")
class PostManager(models.Manager):
def get_query_set(self):
return PostQuerySet(self.model)
def __getattr__(self, attr, *args):
# see https://code.djangoproject.com/ticket/15062 for details
if attr.startswith("_"):
raise AttributeError
return getattr(self.get_query_set(), attr, *args)
class Post(models.Model):
# field definitions...
objects = PostManager()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment