Created
March 13, 2013 13:57
-
-
Save chrislawlor/5152331 to your computer and use it in GitHub Desktop.
Chainable Model Manager boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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