Crosstab Reports

Developer guide for the Enezaframework reporting engine. Part of the Add Report series.

Table of Contents

  1. Overview
  2. How a Crosstab Is Built
  3. Attributes
  4. Example
  5. Validation
  6. Related Guides

Overview

CrosstabReportView produces a two-dimensional pivot table: one axis is group_by, the other is crosstab_field, and each cell holds a computation (for example, __total__).


How a Crosstab Is Built

flowchart LR
    ROWS["Source rows<br/>report_model queryset"] --> GROUP["Group by<br/>group_by"]
    ROWS --> PIVOT["Pivot by<br/>crosstab_field"]
    GROUP --> CELLS["Cell values<br/>crosstab_columns"]
    PIVOT --> CELLS
    CELLS --> REM{"Include remainder?"}
    REM -->|"Yes (default)"| OTHER["Add 'other' column"]
    REM -->|"No"| OUT["Pivot table"]
    OTHER --> OUT

Attributes

Attribute Required Description
report_model Yes Django model for the data source.
group_by Recommended Field used for the row axis.
crosstab_field Yes FK or char field to pivot on (for example "product").
crosstab_columns Yes Computation fields per crosstab cell.
crosstab_ids No Restrict the pivot to specific IDs.
crosstab_ids_custom_filters No Custom filters applied to the pivot IDs.
crosstab_compute_remainder No Include an "other" column (default: True).
crosstab_precomputed No Read pre-aggregated values from the database.
date_field No Date field for date-range filtering.
columns No Extra leading columns (for example ["region__name"]).

Example

python
from enezareporting.report_base import CrosstabReportView
from enezareporting.parameters import ParameterSet, DateRangeParam
from myapp.models import SalesLine


class RegionByProduct(CrosstabReportView):
    report_title = "Region by Product"
    report_model = SalesLine
    date_field = "date_placed"
    group_by = "region"
    crosstab_field = "product"
    crosstab_columns = ["__total__"]
    columns = ["region__name"]
    parameter_spec = ParameterSet(
        DateRangeParam(name="date_range", start_field="date_placed"),
    )

Validation

CrosstabReportView validates at class-creation time. Missing crosstab_field or crosstab_columns raises ImproperlyConfigured:

text
ImproperlyConfigured: <ClassName>: crosstab_field must be provided for CrosstabReport
ImproperlyConfigured: <ClassName>: crosstab_columns must be provided for CrosstabReport