python - I can't access QuerySet in Manager's init method in Django -
i'd queryset of model in manager's __init__ method setup pagination queryset results. reason why want setup pagination in __init__ method because have lots of simple methods in manager getpage , getnumberofpages etc. simplify abstraction , don't want duplicate code setup paginator across these methods.
for example, let's there article model, has custom articlemanager , looks this:
class articlemanager(models.manager): def __init__(self): super(articlemanager, self).__init__() allobjects = super(articlemanager, self).get_queryset() self.articlepaginator = paginator(allobjects, 10) class article(models.model): # blah blah # model fields everywhere objects = articlemanager() fourth line of code super(articlemanager, self).get_queryset() returns attributeerror exception:
attributeerror: 'nonetype' object has no attribute '_meta' i guess should have done more initialize manager, i'm not sure is. i've not found alike want in django docs or in other stackoverflow questions. guess there might wrong in approach, if that's case i'd grateful if point out.
you're calling parent's get_query() method. you're not overriding there's no point, call on self. better yet, make operation lazy. properties option:
class articlemanager(models.manager): @property def article_paginator(self): return paginator(self.get_queryset(), 10) then can access via article.objects.article_paginator.
Comments
Post a Comment