0

I have a MySQL query that I need help converting to use django's ORM

select * from TABLE where ID=10 group by LOCATION order by DATE DESC

Thus far I have TABLE.objects.filter(id='10').order_by('-date') but I am unsure how to do the group by in Django.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Matthew
  • 9
  • 2
  • 1
    What have you tried so far? For your information you can remove the nested query making it more efficient by simply doing `SELECT * FROM TABLE where ID=10 group by LOCATION order by DATE desc` – Matt Seymour Jun 22 '16 at 19:13
  • Thanks! Thus far I have TABLE.objects.filter(id='10').order_by('-date') but I am unsure how to do the group by in Django. – Matthew Jun 22 '16 at 19:17
  • There isn't really a group by in django at the minute on a database level (since django returns querysets rather than results). There is the option to do it on the template side if that helps? – Sayse Jun 22 '16 at 20:49

1 Answers1

0

You will get your answer here aggregation in Django

The topic guide on Django’s database-abstraction API described the way that you can use Django queries that create, retrieve, update and delete individual objects. However, sometimes you will need to retrieve values that are derived by summarizing or aggregating a collection of objects. This topic guide describes the ways that aggregate values can be generated and returned using Django queries.

For some reference on usage : How to query as GROUP BY in django?

Community
  • 1
  • 1
ofnowhere
  • 1,114
  • 2
  • 16
  • 40