Skip to content

Instantly share code, notes, and snippets.

@shihweilo
Created March 1, 2016 16:47
Show Gist options
  • Save shihweilo/f3a22c7fc22e986822e3 to your computer and use it in GitHub Desktop.
Save shihweilo/f3a22c7fc22e986822e3 to your computer and use it in GitHub Desktop.
Django: Useful default manager override
# -*- coding: utf-8 -*-
# copied from django-cms
# https://github.com/divio/django-cms/blob/develop/cms/models/metaclasses.py
from cms.publisher.manager import PublisherManager
from django.db.models.base import ModelBase
class PageMetaClass(ModelBase):
def __new__(cls, name, bases, attrs):
super_new = super(PageMetaClass, cls).__new__
if 'objects' in attrs:
if not isinstance(attrs['objects'], PublisherManager):
raise ValueError("Model %s extends Publisher, "
"so its 'objects' manager must be "
"a subclass of publisher.PublisherManager") % (name,)
else:
attrs['objects'] = PublisherManager()
return super_new(cls, name, bases, attrs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment