1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

12552 add back add_related_count

This commit is contained in:
Arthur
2023-05-24 11:13:43 -07:00
parent 71a74e410d
commit a480a4c23b
6 changed files with 225 additions and 211 deletions

View File

@ -23,3 +23,41 @@ class TreeManager(Manager.from_queryset(TreeQuerySet), TreeManager_):
"""
_with_tree_fields = True
def add_related_count(
self,
queryset,
rel_model,
rel_field,
count_attr,
cumulative=False,
extra_filters={},
):
"""
Adds a related item count to a given ``QuerySet`` using its
``extra`` method, for a ``Model`` class which has a relation to
this ``Manager``'s ``Model`` class.
Arguments:
``rel_model``
A ``Model`` class which has a relation to this `Manager``'s
``Model`` class.
``rel_field``
The name of the field in ``rel_model`` which holds the
relation.
``count_attr``
The name of an attribute which should be added to each item in
this ``QuerySet``, containing a count of how many instances
of ``rel_model`` are related to it through ``rel_field``.
``cumulative``
If ``True``, the count will be for each item and all of its
descendants, otherwise it will be for each item itself.
``extra_filters``
Dict with aditional parameters filtering the related queryset.
"""
return queryset