django查询中的复杂注释

时间:2016-11-04 15:54:58

标签: django

现状:

model.py

class Foo(models.model):
  name =  models.CharField(max_length = 50, blank = True, unique = True)

class Item(models.model):
  foo = models.ForeignKey('Foo')
  number =  models.PositiveIntegerField()

views.py

def index(request):
  stats= Foo.objects.filter(somecond=somevalue).\
  annotate(count=Count('item__number'), max=Max('item__number'))

  return render_to_response('stats.html', {'stats' : stats})

预期的结果是具有附加字段的对象列表Foo:包含与Foo和max相关的项目数的count - 项目的最大数量。

示例:

Item1: Foo1, 12
Item2: Foo2, 100
Item3: Foo1, 10
Item4: Foo2, 78
Item5: Foo1, 6

结果:

统计数据:

Foo1, 3, 12
Foo2, 2, 100

奥莱特。 现在我想获得更多:

class sell(models.Model):
  foo = models.ForeignKey('Foo')
  value = models.DecimalField(max_digits=10,decimal_places=2)

填写:

sell1: Foo1, 12.50
sell2: Foo2, 12.40
sell3: Foo1,  4.12
sell4: Foo2, 10.00



stats=Foo.objects.filter(somecond=somevalue).\
annotate(Count('item__number'), Max('item__number'), Sum('sell__value'))

我的期望是:

统计数据:

Foo1, 3, 12, 16.62
Foo2, 2, 100, 22.40

但结果是项目和值的数量增加(例如,如果卖出元素为1,并且有4个Foos,则总和为4 * sell.value)

我也尝试了Sum(' sell__value',distinct = True),但没有区别。

我想要它用于模板,我想用循环法做

{% for foo in stats %}
<h2>{{ foo.name }} </h2>
{{foo.count}}/{{foo.max}} 
sum of sell related with foo
{{foo.sum}}

我希望将总和插入统计数据。

2 个答案:

答案 0 :(得分:0)

您可以找到https://stackoverflow.com/a/40443262/354420

的一种可能性

您必须使用自己的QuerySet创建自己的Manager:

然后

both = Foo.objects.annotate_sum(Sell, 'value')

答案 1 :(得分:0)

我选择解决方案

stats=Foo.objects.filter(somecond=somevalue).annotate(Count('item__number'), Max('item__number')).in_bulk()
sells=Foo.objects.filter(somecond=somevalue).annotate(Sum('sell__value')).in_bulk()
for key, stat in stats.items():
  setattr(stat,'sell__value__sum',sells[key].sell__value__sum)

我知道in_bulk()调用两个sqls,但这对我来说不是问题。我的另一个答案是调用一个sql。

相关问题