Python Django:注释多个查询

Python Django:注释多个查询,python,django,Python,Django,这是我的密码 data1 = Data.objects.filter(...).annotate(Max('receiver')).order_by('-receiver__max') data2 = Data.objects.filter(...).annotate(Max('sender')).order_by('-sender__max') 如何在一个查询中组合这两个查询?您应该能够很好地组合它,如果您只对最大值感兴趣,那么就没有必要也按排序。你应该能够做到 data = Data.o

这是我的密码

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

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

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

您应该能够很好地组合它,如果您只对最大值感兴趣,那么就没有必要也按排序。你应该能够做到

data = Data.objects.filter(...).annotate(Max('receiver'), Max('sender'))
它应该返回类似的结果

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

工作非常好。谢谢你,先生!