def add_related_count(self, queryset, rel_model, rel_field, count_attr,
                          cumulative=False):
        opts = self.model._meta
        if cumulative:
            subquery = CUMULATIVE_COUNT_SUBQUERY % {
                'rel_table': qn(rel_model._meta.db_table),
                'mptt_fk': qn(rel_model._meta.get_field(rel_field).column),
                'mptt_table': qn(opts.db_table),
                'mptt_pk': qn(opts.pk.column),
                'tree_id': qn(opts.get_field(self.tree_id_attr).column),
                'left': qn(opts.get_field(self.left_attr).column),
                'right': qn(opts.get_field(self.right_attr).column),
            }
        else:
            subquery = COUNT_SUBQUERY % {
                'rel_table': qn(rel_model._meta.db_table),
                'mptt_fk': qn(rel_model._meta.get_field(rel_field).column),
                'mptt_table': qn(opts.db_table),
                'mptt_pk': qn(opts.pk.column),
            }
        return queryset.extra(select={count_attr: subquery})
Returns:
dataset/ETHPy150Open agiliq/django-socialnews/socialnews/mptt/managers.py/TreeManager.add_related_count--crossover with--dataset/ETHPy150Open meraki-analytics/cassiopeia/cassiopeia/type/core/match.py/ParticipantTimeline.ancient_golem_assists_per_min_counts