user8733268
user8733268

Reputation:

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?

Upvotes: 1

Views: 63

Answers (1)

markwalker_
markwalker_

Reputation: 12849

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}

Upvotes: 1

Related Questions