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
| Attribute | Type | Description |
|---|---|---|
| edges | dict = {} | A mapping of source instances to lists of target instances representing the relationships discovered during collection. |
| protected | set = set() | A set of model instances that cannot be deleted due to ProtectedError or RestrictedError constraints encountered during collection. |
| model_objs | defaultdict = 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
| Name | Type | Description |
|---|---|---|
| *args | any | Variable length argument list passed to the parent Collector class. |
| force_collection | boolean = True | Flag to determine if collection should be forced regardless of certain constraints. |
| **kwargs | any | Arbitrary 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
| Name | Type | Description |
|---|---|---|
| source | object | The parent object or origin of the relationship; use None for root-level objects. |
| target | object | The dependent object being linked to the source. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| objs | iterable | A collection of model instances to begin the collection process from. |
| source | object | The parent instance that triggered the collection of the current objects. |
| source_attr | string | The attribute name or string template representing the relationship from the source to the objects. |
Returns
| Type | Description |
|---|---|
object | The result of the parent collector's collection process, typically representing the set of objects identified for deletion. |
related_objects()
@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
| Name | Type | Description |
|---|---|---|
| related_model | [Model](../../../db/models/base/model.md?sid=django_db_models_base_model) | The Django model class to query for related instances. |
| related_fields | list | A list of field objects used to define the relationship and optimize the query via select_related. |
| objs | iterable | The base objects for which related instances are being discovered. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| format_callback | callable | An optional function to format each object in the resulting list, such as returning its verbose name. |
Returns
| Type | Description |
|---|---|
list | A hierarchical list structure representing the tree of collected objects and their dependencies. |