Is there a way to select a list of group by columns? 
The aggregation methods seem only apply to a single model but I want to use 
fields from different models to group by. 

select org.id as organization_id,
       dep.id as department_id,
       cat."catId",
       max(cat.name) as name,
       max(cat."nameDisplay") as nameDisplay,
       count(depItem.id) as itemCount
  from communitynetwork_listcategory as cat 
       inner join communitynetwork_listitem as li on
         li.category_id = cat."catId"
       inner join communitynetwork_organizationdepartmentitem as depItem on
         depItem.item_id = li.id
       inner join communitynetwork_organizationdepartment as dep on
         dep.id = depItem.department_id
       inner join communitynetwork_organization as org on
         org.id = dep.organization_id and
         org.active = True                  
 where cat.name like '%service%' 
    or cat."nameDisplay" like '%service%' 
group by org.id, dep.id, cat."catId"
order by org.name, dep.name, cat.order

This is what I tried to do with a QuerySet that didn't work:

OrganizationDepartmentItem.objects.filter(
department__organization__active=True
    ).filter(Q(item__category__name=searchValue) | Q(
item__category__nameDisplay=searchValue)
    ).order_by('department__organization', 'department__name', 
'item__category__order'
    ).values('department__organization', 'department', 'item__category'
    ).aggregate(Max('item__category__name'), 
Max('item__category__nameDisplay'), Count('item'))
            

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/zY3x-atpBYIJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to