Skip to content

Grouping

For data to be grouped when being returned from a resource the resource must extend from CubedReportResource. You must also have at least one field in your resource which is not part of the unique - for example, fm_revenue. If your resource does not have a field which is not part of the unique (groupby=True), the groupby will not work as expected.

Applying a Group By

Grouping needs to be set on a field by field bases on the resource like so:

1

class Meta(ReportMeta): queryset = ReportPPCDashboard.objects.prefetch_related('product') resource_name = "report-ppc-dashboard" config_fields = BaseFields(show_totals=True, full_ordering=True) config_fields.install('product', IntegerField(), filtering=ALL_WITH_RELATIONS, groupby=True) config_fields.install('date', DateField(), filtering=ALL_WITH_RELATIONS, groupby=True) config_fields.install('ppc_cost', MoneyField()) config_fields.install('fm_revenue', MoneyField()) config_fields.install('fm_sales', IntegerField()) config_fields.install('impressions', IntegerField())

The field(s) can then be grouped on by sending group_by=<field1,field2,field3> up to the server, the groups will mostly likely be applied in the order that you supplied unless Django knows better... If you have found that another group has been added that you have not asked for then something has gone wrong with your expressions.

  • If you have not installed a field on the resource and then refrence it in an expression it will not have been given the Sum() wrapper and django will think it needs to be grouped.

  • If you have not followed the expressions guide and have incorrectly created a CombinedExpression that resolves to an Aggregate django will think it needs to be grouped.

Overriding the default grouping

Sometimes you may want to override the way that a date is grouped by, this can be done with the timeframe queryparam, pass in the name of the field you wish to override, and then the "kind" of date function you want to apply to it: timeframe="sales_date__day" detailed info of "kinds" can be found here, and a quick list is below:

  • year
  • quarter
  • month
  • week
  • day
  • hour
  • minute
  • second

Warning: if timeframe is in the request object this will also stop custom ordering from working!