ComputationField API

class slick_reporting.fields.ComputationField(*args, **kwargs)[source]

Computation field responsible for making the calculation unit

name = ''

The name to be used in the ReportGenerator

calculation_field = 'value'

the Field to compute on

calculation_method = <class 'django.db.models.aggregates.Sum'>

The computation Method

verbose_name = None

Verbose name to be used in front end when needed

requires = None

This can be a list of sibling classes, they will be asked to compute and their value would be available to you in the resolve method requires = [BasicCalculationA, BasicCalculationB]

type = 'number'

Just a string describing what this computation field return, usually passed to frontend

Below are some data passed by the ReportGenerator, for extra manipulation, you can change them

report_model = None

The model on which the computation would occur

group_by = None
plus_side_q = None
minus_side_q = None

You can customize those methods for maximum control where you can do pretty much whatever you want.

prepare(q_filters: list | object = None, kwargs_filters: dict = None, main_queryset=None, group_by: str = None, prevent_group_by=None, **kwargs)[source]

This is the first hook where you can customize the calculation away from the Django Query aggregation method This method is called with all available arguments, so you can prepare the results for the whole set and save it in a local cache (like self._cache) . The flow will later call the method resolve, giving you the id, for you to return it respective calculation

Parameters:
  • q_filters

  • kwargs_filters

  • main_queryset

  • group_by

  • prevent_group_by

  • kwargs

Returns:

resolve(prepared_results, required_computation_results: dict, current_pk, current_row=None) float[source]

Reponsible for getting the exact data from the prepared value :param prepared_results: the returned data from prepare :param required_computation_results: the returned data from prepare :param current_pk: he value of group by id :param current_row: the row in iteration :return: a solid number or value

get_dependency_value(current_obj, name)[source]

Get the values of the ReportFields specified in requires

Parameters:
  • current_obj – the current object which we want the calculation for

  • name – the name of the specific dependency you want.

Returns:

a dict containing dependencies names as keys and their calculation as values or a specific value if name is specified.