Django:注释多个查询

时间:2017-10-29 19:21:03

标签: python django

这是我的代码,

data1 = Data.objects.filter(...).annotate(Max('receiver')).order_by('-receiver__max')

data2 = Data.objects.filter(...).annotate(Max('sender')).order_by('-sender__max')

如何在一个查询中组合这两个查询?

1 个答案:

答案 0 :(得分:1)

你应该能够很好地结合它,如果你只对最大值感兴趣,那么也不需要order_by。你应该能做到的;

data = Data.objects.filter(...).annotate(Max('receiver'), Max('sender'))

哪些应该返回类似的内容;

{'receiver__max': 10, 'sender__max': 12}