新版本的django-model-utils没有PassThroughManager

时间:2016-08-29 12:25:07

标签: python django database model django-queryset

我使用Django 1.9.9那么如何编辑我的模型而不是PassThroughManager?

from model_utils.managers import PassThroughManager

class TopicQuerySet(QuerySet):

    def get_topic_today(self):
        return self.filter(created_at__day=datetime.date.today()).order_by('title')

    def get_topic_popular(self):
        return self.annotate(entry_count=Count('entry')).order_by('-entry_count')


class Topic(TimeStampModel):

    objects = PassThroughManager.for_queryset_class(TopicQuerySet)()

我应该尝试使用GeoManager吗?

1 个答案:

答案 0 :(得分:1)

较新版本的Django(我相信1.9+)有一些内置的( model_utils PassThroughManager 之类的功能。像这样重写你应该得到相同的概念:

from django.db.models import Manager
from django.db.models.query import QuerySet

class TopicManager(Manager):
    pass

class TopicQuerySet(QuerySet):

    def get_topic_today(self):
        return self.filter(created_at__day=datetime.date.today()).order_by('title')

    def get_topic_popular(self):
        return self.annotate(entry_count=Count('entry')).order_by('-entry_count')

class Topic(TimeStampModel):
    objects = TopicManager.from_queryset(TopicQuerySet)()