简体   繁体   中英

Django: Annotate Multiple Queries

Here's my code,

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

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

How can I combine these 2 queries in just one single Query?

You should be able to combine it quite nicely, and if you're only interested in the max values then there's no need to also order_by . You should just be able to do;

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

Which should return something like;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM