How to specify a group by field set?

11 views
Skip to first unread message

Joshua Russo

unread,
Nov 22, 2011, 2:45:48 PM11/22/11
to django...@googlegroups.com
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'))
            
Reply all
Reply to author
Forward
0 new messages