簡體   English   中英

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

[英]New version of django-model-utils doesn't have PassThroughManager

我使用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嗎?

較新版本的Django(我認為是1.9+)具有一些內置的( model_utilsPassThroughManager之類的功能。 像這樣重寫,您應該得到相同的概念:

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)()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM