Django ORM:计算相关项目的子集
我正在寻找一种方法来使用相关项目子集的计数来注释查询集。以下是我的模型的子集:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们看看。
首先,找到具体的俱乐部。
接下来,过滤所有曾在该俱乐部练习过的人。
现在,用计数进行注释。
编辑
我能够在我的测试设置中成功地完成这项工作:Django 1.2.3、Python 2.6.4、Postgresql 8.4、Ubuntu Karmic。
PS:为字段使用小写名称是一个好主意™。这使得使用双下划线 (
__
) 语法来链接字段变得更加容易。例如,在您的情况下,Django 自动为每个Person
创建practicerecord
。当您尝试通过此字段访问PracticeRecord
的其他字段时,您必须记住使用标题大小写。如果您使用小写名称,您可以写:
这看起来更加统一。
PPS:它是
Count('practicerecord')
(注意小写)。Let us see.
First, find the specific club.
Next, filter all Persons who have practiced at this club.
Now, annotate with the count.
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 createspracticerecord
for eachPerson
. When you try to access other fields ofPracticeRecord
through this field you have to remember to use title case.If you had used lower case names, you could have written:
which looks far more uniform.
PPS: It is
Count('practicerecord')
(note the lower case).恐怕原始 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.