0

I want to write a query like SELECT * FROM users GROUP BY some_attribute. How can I do that using Django ORM?

User.objects.all().values('some_attribute').annotate(count=Count('*'))

doesn't work, because it just selects some_attribute, instead of * - all.

I need it using the ORM, I don't want to write raw statement.

Tomas
  • 59
  • 1
  • 6
  • 2
    `SELECT * FROM ... GROUP BY ...` is not a valid SQL query. You can only use aggregates, or fields in the `GROUP BY` part in the `SELECT` part. – Willem Van Onsem Jun 22 '20 at 15:22
  • there is already an answer : https://stackoverflow.com/questions/629551/how-to-query-as-group-by-in-django – ensarman Jun 22 '20 at 15:26

0 Answers0