Skip to main content

NestedObjects

This class facilitates the discovery and organization of related database objects into a hierarchical structure. It tracks relationships between instances using an internal edge graph and provides mechanisms to handle protected or restricted dependencies during collection. The class primarily offers functionality to represent these discovered relationships as a nested list for visualization or processing.

Attributes

AttributeTypeDescription
edgesdict = {}A mapping of source instances to lists of target instances representing the relationships discovered during collection.
protectedset = set()A set of model instances that cannot be deleted due to ProtectedError or RestrictedError constraints encountered during collection.
model_objsdefaultdict = defaultdict(set)A dictionary grouping all collected model instances by their respective model class.

Constructor

Signature

def NestedObjects(
*args: any,
force_collection: boolean = True,
**kwargs: any
) - > null

Parameters

NameTypeDescription
*argsanyVariable length argument list passed to the parent Collector class.
force_collectionboolean = TrueFlag to determine if collection should be forced regardless of certain constraints.
**kwargsanyArbitrary keyword arguments passed to the parent Collector class.

Methods


add_edge()

@classmethod
def add_edge(
source: object,
target: object
) - > null

Registers a relationship between a source object and a target object within the internal dependency graph.

Parameters

NameTypeDescription
sourceobjectThe parent object or origin of the relationship; use None for root-level objects.
targetobjectThe dependent object being linked to the source.

Returns

TypeDescription
null

collect()

@classmethod
def collect(
objs: iterable,
source: object,
source_attr: string
) - > object

Traverses and gathers related objects while building an edge map of dependencies and tracking protected or restricted instances that cannot be deleted.

Parameters

NameTypeDescription
objsiterableA collection of model instances to begin the collection process from.
sourceobjectThe parent instance that triggered the collection of the current objects.
source_attrstringThe attribute name or string template representing the relationship from the source to the objects.

Returns

TypeDescription
objectThe result of the parent collector's collection process, typically representing the set of objects identified for deletion.

@classmethod
def related_objects(
related_model: [Model](../../../db/models/base/model.md?sid=django_db_models_base_model),
related_fields: list,
objs: iterable
) - > [QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)

Fetches a QuerySet of related objects for the given model and fields, optimized with select_related to reduce database hits during traversal.

Parameters

NameTypeDescription
related_model[Model](../../../db/models/base/model.md?sid=django_db_models_base_model)The Django model class to query for related instances.
related_fieldslistA list of field objects used to define the relationship and optimize the query via select_related.
objsiterableThe base objects for which related instances are being discovered.

Returns

TypeDescription
[QuerySet](../../../db/models/query/queryset.md?sid=django_db_models_query_queryset)A Django QuerySet containing the related objects with their foreign key relationships pre-fetched.

nested()

@classmethod
def nested(
format_callback: callable
) - > list

Return the graph as a nested list.

Parameters

NameTypeDescription
format_callbackcallableAn optional function to format each object in the resulting list, such as returning its verbose name.

Returns

TypeDescription
listA hierarchical list structure representing the tree of collected objects and their dependencies.