I would like to be able to programmatically decide which aggregator to use in a django annotate function. That is, given a statement like the following…
partsPerMachine = Machine.objects.annotate(num_parts=Count('parts'))
… I would like to be able to determine at run-time which aggregator to use. I could be summing/counting/averaging/etc.’ing parts and I won’t know which ahead of time.
I don’t quite understand the python magic that django’s authors have used to create this syntax. If this were just a case of named parameters, I could use Python’s ** syntax to pass a dictionary of parameters. But this seems to be something else…
The aggregators are simply Python classes. One easy way to select the desired aggregator at runtime would be to put them in a dictionary, then select the desired aggregator by key: