Django 经理

发布于 2024-09-03 06:51:31 字数 883 浏览 8 评论 0原文

我有以下模型代码:

from django.db import models
from categories.models import Category

class MusicManager(models.Manager):
    def get_query_set(self):
        return super(MusicManager, self).get_query_set().filter(category='Music')
    def count_music(self):
        return self.all().count()

class SportManager(models.Manager):
    def get_query_set(self):
        return super(MusicManager, self).get_query_set().filter(category='Sport')        

class Event(models.Model): 
    title = models.CharField(max_length=120)
    category = models.ForeignKey(Category)
    objects = models.Manager()
    music = MusicManager()
    sport = SportManager()

现在通过注册 MusicManager() 和 SportManager() 我可以调用 Event.music.all() 和 Event.sport.all() 查询。但如何创建 Event.music.count() ?我应该在 MusicManager 的 count_music() 函数中调用 self.all() 来仅查询具有“音乐”类别的元素,还是仍然需要先过滤它们以搜索类别?

I have the following models code :

from django.db import models
from categories.models import Category

class MusicManager(models.Manager):
    def get_query_set(self):
        return super(MusicManager, self).get_query_set().filter(category='Music')
    def count_music(self):
        return self.all().count()

class SportManager(models.Manager):
    def get_query_set(self):
        return super(MusicManager, self).get_query_set().filter(category='Sport')        

class Event(models.Model): 
    title = models.CharField(max_length=120)
    category = models.ForeignKey(Category)
    objects = models.Manager()
    music = MusicManager()
    sport = SportManager()

Now by registering MusicManager() and SportManager() I am able to call Event.music.all() and Event.sport.all() queries. But how can I create Event.music.count() ? Should I call self.all() in count_music() function of MusicManager to query only on elements with 'Music' category or do I still need to filter through them in search for category first ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-09-10 06:51:31

您可以将管理器视为查询的“起点” - 您可以继续链接过滤器,就像从默认管理器开始一样。

例如,Event.objects.filter(category='Music').filter(title='Beatles Concert') 在功能上等同于 Event.music.filter(title='Beatles Concert') ')

因此,正如 Daniel 所说,您实际上不需要做任何特殊的事情,只需选择一个自定义管理器而不是对象,然后从那里开始。

You can think of a manager as a 'starting point' for a query - you can continue to chain filters just as if you'd started out with the default manager.

For example, Event.objects.filter(category='Music').filter(title='Beatles Concert') is functionally equivalent to Event.music.filter(title='Beatles Concert')

So, as Daniel says, you don't really need to do anything special, just choose one of your custom managers instead of objects and go from there.

饮惑 2024-09-10 06:51:31

您不需要执行任何操作(并且您的 count_music 方法是不必要的)。 count() 方法将使用 get_query_set 定义的现有查询。

You don't need to do anything (and your count_music method is unnecessary). The count() method will use the existing query as defined by get_query_set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文