views:

40

answers:

1

How do I inherit a ModelManager?

class Content(models.Model):
    name = models.CharField(max_length=255, verbose_name='Name 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?

+2  A: 

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.

Gabriel Hurley
+1 for the docs link. Or make Content an abstract model :)
oggy
yes, that helped thank you.The problem is that I always think my idea can't be the smartest way to approach this problem :-/.I thought of just giving the BlogCatalog an ContentActiveManager but I wasn't sure whether this was considered 'good code'.Thank you for the help :).
self.name
yep, this is the way the django devs have set things up. So as long as Content isn't an ABC this is considered the "right" way to do things ;-)
Gabriel Hurley