Skip to main content

Node

A single internal node in the tree graph. A Node should be viewed as a connection (the root) with the children being either leaf nodes or other Node instances.

Attributes

AttributeTypeDescription
defaultstring = DEFAULTStandard connector type. Clients usually won't use this at all and subclasses will usually override the value.
childrenlist = []A list of leaf nodes or other Node instances that represent the branches of the current tree graph.
connectorstring = DEFAULTThe logical operator or connection type used to join the children of this node.
negatedboolean = falseA boolean flag indicating whether the sense of the root connector is inverted.
copycallableA reference to the copy method used to create a shallow copy of the Node instance.

Constructor

Signature

def Node(
children: list = None,
connector: str = None,
negated: bool = False
) - > null

Parameters

NameTypeDescription
childrenlist = NoneA list of child nodes to be associated with this node.
connectorstr = NoneThe connector type for the node; defaults to the class 'default' value if not provided.
negatedbool = FalseA boolean flag indicating whether the node's logic is negated.

Methods


create()

@classmethod
def create(
children: list = None,
connector: str = None,
negated: bool = False
) - > [Node](node.md?sid=django_utils_tree_node)

Create a new instance using Node() instead of init() as some subclasses, e.g. django.db.models.query_utils.Q, may implement a custom init() with a signature that conflicts with the one defined in Node.init().

Parameters

NameTypeDescription
childrenlist = NoneA list of leaf nodes or other Node instances to be added as children
connectorstr = NoneThe logical connector string used to join children; defaults to the class's default connector
negatedbool = FalseA flag indicating whether the sense of the root connector is inverted

Returns

TypeDescription
[Node](node.md?sid=django_utils_tree_node)A new instance of the class or subclass with the specified properties

add()

@classmethod
def add(
data: object,
conn_type: str
) - > object

Combine this tree and the data represented by data using the connector conn_type. The combine is done by squashing the node other away if possible.

Parameters

NameTypeDescription
dataobjectThe new data or Node instance to be merged into the current tree
conn_typestrThe connector type to use for the combination; may trigger a tree restructuring if it differs from the current connector

Returns

TypeDescription
objectA node which can be used in place of data regardless if the node other got squashed or not

negate()

@classmethod
def negate()

Negate the sense of the root connector.