Django ORM:计算相关项目的子集

发布于 2024-09-27 01:30:19 字数 886 浏览 2 评论 0原文

我正在寻找一种方法来使用相关项目子集的计数来注释查询集。以下是我的模型的子集:

class Person(models.Model):
    Name = models.CharField(max_length = 255)
    PracticeAttended = models.ManyToManyField('Practice',
                                              through = 'PracticeRecord')

class Club(models.Model):
    Name = models.CharField(max_length = 255)
    Slug = models.SlugField()
    Members = models.ManyToManyField('Person')

class PracticeRecord(PersonRecord):
    Person = models.ForeignKey(Person)
    Practice = models.ForeignKey(Practice)

class Practice(models.Model):
    Club = models.ForeignKey(Club, default = None, null = True)
    Date = models.DateField()

我正在创建一个查询集,用于注释一个人参加的俱乐部特定练习的数量。我已经可以通过查询 Person.objects.all().annotate(Count('PracticeRecord')) 找到该人的实践总数,

但是我想以某种方式注释一个人参加特定俱乐部的做法。

我更喜欢使用 django ORM,而不必编写原始 SQL。

谢谢。

I am looking to find a way to annotate a queryset with the counts of a subset of related items. Below is a subset of my models:

class Person(models.Model):
    Name = models.CharField(max_length = 255)
    PracticeAttended = models.ManyToManyField('Practice',
                                              through = 'PracticeRecord')

class Club(models.Model):
    Name = models.CharField(max_length = 255)
    Slug = models.SlugField()
    Members = models.ManyToManyField('Person')

class PracticeRecord(PersonRecord):
    Person = models.ForeignKey(Person)
    Practice = models.ForeignKey(Practice)

class Practice(models.Model):
    Club = models.ForeignKey(Club, default = None, null = True)
    Date = models.DateField()

I'm looking to make a queryset which annotates the number of club specific practices attended by a person. I can already find the total number of practices by that person with a query of Person.objects.all().annotate(Count('PracticeRecord'))

However I would like someway to annotate the number of practices that a person attends for a specific club.

I would prefer something using the django ORM without having to resort to writing raw SQL.

Thanks.

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

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

发布评论

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

评论(2

白况 2024-10-04 01:30:19

但是,我想以某种方式注释一个人参加特定俱乐部的练习次数。

让我们看看。

首先,找到具体的俱乐部。

club = Club.objects.get(**conditions)

接下来,过滤所有曾在该俱乐部练习过的人。

persons = Person.objects.filter(practicerecord__Practice__Club = club)

现在,用计数进行注释。

q = persons.annotate(count = Count('practicerecord'))

编辑

我能够在我的测试设置中成功地完成这项工作:Django 1.2.3、Python 2.6.4、Postgresql 8.4、Ubuntu Karmic。

PS:为字段使用小写名称是一个好主意™。这使得使用双下划线 (__) 语法来链接字段变得更加容易。例如,在您的情况下,Django 自动为每个Person 创建practicerecord。当您尝试通过此字段访问 PracticeRecord 的其他字段时,您必须记住使用标题大小写。

如果您使用小写名称,您可以写:

persons = Person.objects.filter(practicerecord__practice__club = club)
#                                               ^^        ^^  

这看起来更加统一。

PPS:它是Count('practicerecord')(注意小写)。

However I would like someway to annotate the number of practices that a person attends for a specific club.

Let us see.

First, find the specific club.

club = Club.objects.get(**conditions)

Next, filter all Persons who have practiced at this club.

persons = Person.objects.filter(practicerecord__Practice__Club = club)

Now, annotate with the count.

q = persons.annotate(count = Count('practicerecord'))

Edit

I was able to successfully make this work in my test setup: Django 1.2.3, Python 2.6.4, Postgresql 8.4, Ubuntu Karmic.

PS: It is a Good Idea™ to use lower case names for the fields. This makes it far easier to use the double underscore (__) syntax to chain fields. For e.g. in your case Django automatically creates practicerecord for each Person. When you try to access other fields of PracticeRecord through this field you have to remember to use title case.

If you had used lower case names, you could have written:

persons = Person.objects.filter(practicerecord__practice__club = club)
#                                               ^^        ^^  

which looks far more uniform.

PPS: It is Count('practicerecord') (note the lower case).

小霸王臭丫头 2024-10-04 01:30:19

恐怕原始 sql 是这里唯一的选择。不管怎样,如果你把它交给模型经理,它并不那么可怕和难以管理。

I'm afraid that raw sql is the only option here. Anyway it's not that scary and hard to manage if you put it to model manager.

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