【发布时间】:2016-11-29 04:57:39
【问题描述】:
我有一个模型“A”的查询集,我正在尝试对其进行一些值/注释计算。模型“A”与模型 B 具有多对多关系。从模型“A”中,我希望能够基于“B”中的字段访问“B”模型的过滤子集。我将如何更改我的模型“A”以支持这种模式?
虚拟示例:
class Publication(models.Model):
is_digital = models.BooleanField(default=True)
readership = models.IntegerField()
class Article(models.Model):
language = models.CharField()
published_in = models.ManyToManyField(Publication)
# returns a queryset of {publications: total readership} for the articles
$ articles = Articles.objects.filter(language='en')
$ articles.values('published_in').annotate(Sum('readership'))
我想返回关于文章的出版物和总读者人数的查询仅适用于数字出版物。
【问题讨论】: