Created
March 1, 2016 16:47
-
-
Save shihweilo/f3a22c7fc22e986822e3 to your computer and use it in GitHub Desktop.
Django: Useful default manager override
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
# -*- 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