Collecting data from multiple Models is a common flow in Django development. In most of the cases prefetch_related
and select_related
do all of the job for optimizing the queries.
When the models structure become complicated enough and we’ve put our logic for collecting these items in models’ properties we suddenly cannot use prefetch_related
or select_related
anymore. Our View ‘s queries count depends on selected items count with high complexity. The problem is in the selection logic: we’ve implemented an algorithm which calculates something and we cannot prefetch or select all of the objects.
So this talk’s purpose is: To show how to use Django ORM to move the selection logic, the calculations, the aggregations over group of objects from our python code to our database and moving all of the logic for this in one place - the queryset and test it easily.