How do I inherit a ModelManager?
class Content(models.Model):
name = models.CharField(max_length=255, verbose_name='Nam开发者_JAVA百科e des Blogs')
slug = models.SlugField(max_length=80, blank=True)
objects = models.Manager()
active = ContentActiveManager()
class ContentActiveManager(models.Manager):
def get_query_set(self):
return super(ContentActiveManager,self).get_query_set().filter(activated=True,show=True)
class BlogCatalog(Content):
frequency = models.PositiveSmallIntegerField(max_length=2, choices=make_index_based_tuple(l=FREQUENCY), verbose_name='Frequenz',)
blog = BlogCatalog.active.get(pk=1)
blog
is now obviously a Content object.
If I type Catalog.active.get(pk=1) I want a Content object but
If I type BlogCatalog.active.get(pk=1) I want a BlogCatalog object.
How do I achieve this without being redundant?
Django only allows Manager inheritance from an abstract base class. To use the same manager as a non-ABC, you have to declare it explicitly.
Check out the django docs on custom managers and inheritance.
Basically, just do this:
class BlogCatalog(Content):
frequency = models.PositiveSmallIntegerField(max_length=2, choices=make_index_based_tuple(l=FREQUENCY), verbose_name='Frequenz',)
active = ContentActiveManager()
Hope that helps.
the only way I figured out:
class Content:
@staticmethod
def __new__(cls, *args, **kwargs):
super_new = super(Content, cls).__new__(cls, *args, **kwargs)
cls.add_to_class('active', ContentActiveManager())
return super_new
精彩评论