Skip to content

utils

get_relations_sides_and_names(to_field, parent, child)

Determines the names of child and parent relations names, as well as changes one of the sides of the relation into weakref.proxy to model.

Parameters:

Name Type Description Default
to_field ForeignKeyField

field with relation definition

required
parent Model

parent model

required
child Model

child model

required

Returns:

Type Description
tuple["Model", "Model", str, str]

parent, child, child_name, to_name

Source code in ormar/relations/utils.py
Python
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def get_relations_sides_and_names(
    to_field: ForeignKeyField, parent: "Model", child: "Model"
) -> tuple["Model", "Model", str, str]:
    """
    Determines the names of child and parent relations names, as well as
    changes one of the sides of the relation into weakref.proxy to model.

    :param to_field: field with relation definition
    :type to_field: ForeignKeyField
    :param parent: parent model
    :type parent: Model
    :param child: child model
    :type child: Model
    :return: parent, child, child_name, to_name
    :rtype: tuple["Model", "Model", str, str]
    """
    to_name = to_field.name
    child_name = to_field.get_related_name()
    if to_field.virtual:
        child_name, to_name = to_name, child_name
        child, parent = parent, proxy(child)
    else:
        child = proxy(child)
    return parent, child, child_name, to_name

is_dead_proxy(value)

Parameters:

Name Type Description Default
value Any

a related model, or a weak proxy to one

required

Returns:

Type Description
bool

whether the value is a weak proxy to a model that no longer exists

Source code in ormar/relations/utils.py
Python
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def is_dead_proxy(value: Any) -> bool:
    """
    :param value: a related model, or a weak proxy to one
    :type value: Any
    :return: whether the value is a weak proxy to a model that no longer exists
    :rtype: bool
    """
    if not isinstance(value, ProxyTypes):
        return False
    try:
        value.__class__
    except ReferenceError:
        return True
    return False