class GroupByCustomQueryset(ReportView):
report_model = SalesTransaction
report_title = _("Group By Custom Queryset")
report_description = _("Replaces automatic group-by with three crafted querysets (big, small, medium)."
"`format_row()` substitutes readable labels for the row's numeric index.")
date_field = "date"
group_by_custom_querysets = [
SalesTransaction.objects.filter(product__size__in=["big", "extra_big"]),
SalesTransaction.objects.filter(product__size__in=["small", "extra_small"]),
SalesTransaction.objects.filter(product__size="medium"),
]
group_by_custom_querysets_column_verbose_name = _("Product Size")
columns = [
"__index__",
ComputationField.create(Sum, "value", verbose_name=_("Total Sold $"), name="value"),
]
chart_settings = [
Chart(
title="Total sold By Size $",
type=Chart.BAR,
data_source=["value"],
title_source=["__index__"],
),
]
def format_row(self, row_obj):
# Put the verbose names we need instead of the integer index
index = row_obj["__index__"]
if index == 0:
row_obj["__index__"] = "Big"
elif index == 1:
row_obj["__index__"] = "Small"
elif index == 2:
row_obj["__index__"] = "Medium"
return row_obj