-2

I have a model with the following fields

id -> int

vivitor_id -> int

branch_id -> int

date -> datetime

I need to perform the following query in Django. How to do this using Django ORM.

select branch_id from report group by branch_id order by max(date) desc ;
Abhijith Konnayil
  • 4,067
  • 6
  • 23
  • 49

1 Answers1

1

You should use proper Aggregation with values as documented so something in a line of

Report.objects.values('branch_id') 
    .annotate(max_date= Max('date'))
    .order_by('-max_date')
iklinac
  • 14,944
  • 4
  • 28
  • 30