-
-
Notifications
You must be signed in to change notification settings - Fork 31
Description
Hi, we're looking at migrating from django-mptt and I was wondering if there's a recommended strategy for replacing its add_related_count()
TreeManager method. We rely on this to provide a cumulative count of related objects assigned to a nested model.
For example, suppose we have Region instances representing countries, states, and cities, and we have Site instances which can be assigned to any Region. We'd call something like this to annotate a cumulative count of sites for each region:
Region.objects.add_related_count(
Region.objects.all(),
Site,
'region',
'site_count',
cumulative=True
)
Replicating MPTT's approach directly doesn't seem to be feasible, since its querying against concrete database fields: Django's Subquery class doesn't understand tree_path
generated by CTE. (It yields a FieldError: Cannot resolve keyword 'tree_path' into field.
) However I'll admit this is challenging my own skill with SQL.
Has anyone come up with a similar solution using CTE?