I want to execute a simple query, for example:
select *,count('id') from menu_permission group by menu_id
In django format, I tried:
MenuPermission.objects.all().values('menu_id').annotate(Count('id))
It selects only menu_id. Running a query for this:
SELECT `menu_permission`.`menu_id`, COUNT(`menu_permission`.`id`) AS `id__count` FROM `menu_permission` GROUP BY `menu_permission`.`menu_id`
But I need other fields. If I try:
MenuPermission.objects.all().values('id','menu_id').annotate(Count('id))
It adds 'id' to the group by condition.
GROUP BY `menu_permission`.`id`
As a result, I do not get the expected result. How can I get all the objects in groups in just 1 column?
source
share