Skip to content

key_only

Key-only models: instances of a related model with only the fields a relation references (its primary key) set, built when a row holds a foreign key value but the related model itself was not selected.

key_dict_type(*, target, fields) cached

Build the TypedDict of a key of several fields of the target, used as the type of flattened relations ({"tenant_id": 1, "number": 7}) so requests are validated per field and OpenAPI lists the fields.

Parameters:

Name Type Description Default
target type[Model]

model the key belongs to

required
fields tuple[str, ...]

fields of the key

required

Returns:

Type Description
Any

the typed dict class

Source code in ormar/fields/key_only.py
Python
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
@lru_cache(maxsize=1024)
def key_dict_type(*, target: type["Model"], fields: tuple[str, ...]) -> Any:
    """
    Build the ``TypedDict`` of a key of several fields of the target, used as
    the type of flattened relations (``{"tenant_id": 1, "number": 7}``) so
    requests are validated per field and OpenAPI lists the fields.

    :param target: model the key belongs to
    :type target: type[Model]
    :param fields: fields of the key
    :type fields: tuple[str, ...]
    :return: the typed dict class
    :rtype: Any
    """
    model_fields = target.ormar_config.model_fields
    annotations = {
        field: (
            cast("ForeignKeyField", model_fields[field]).flat_value_type()
            if model_fields[field].is_relation
            else model_fields[field].__type__
        )
        for field in fields
    }
    return cast(Any, TypedDict)(f"{target.__name__}Key", annotations)

key_only_instance(*, target, values)

Create an instance of the target with only the given key fields set, skipping validation of the other fields. Required relations of the target get placeholder instances keyed with -1 (see placeholder_key).

Parameters:

Name Type Description Default
target type[Model]

model to instantiate

required
values dict[str, Any]

key field values by field name

required

Returns:

Type Description
Model

key-only instance of the target

Source code in ormar/fields/key_only.py
Python
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def key_only_instance(*, target: type["Model"], values: dict[str, Any]) -> "Model":
    """
    Create an instance of the target with only the given key fields set,
    skipping validation of the other fields. Required relations of the target
    get placeholder instances keyed with ``-1`` (see ``placeholder_key``).

    :param target: model to instantiate
    :type target: type[Model]
    :param values: key field values by field name
    :type values: dict[str, Any]
    :return: key-only instance of the target
    :rtype: Model
    """
    init_dict = {
        **{
            name: key_only_instance(target=field.to, values=placeholder_key(field.to))
            for name, field in target.ormar_config.model_fields.items()
            if field.is_relation
            and not field.nullable
            and not field.virtual
            and name not in values
        },
        **values,
    }
    return cast(
        "Model", target._internal_construct(_pk_only=True, _excluded=None, **init_dict)
    )

key_only_model_type(*, target, fields)

Build the pydantic model used to validate a relation value given as a key-only model, with one field per referenced field of the target.

Parameters:

Name Type Description Default
target type[Model]

model the relation leads to

required
fields tuple[str, ...]

referenced fields of the target

required

Returns:

Type Description
type[BaseModel]

pydantic model holding only the referenced fields

Source code in ormar/fields/key_only.py
Python
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def key_only_model_type(
    *, target: type["Model"], fields: tuple[str, ...]
) -> type[BaseModel]:
    """
    Build the pydantic model used to validate a relation value given as a
    key-only model, with one field per referenced field of the target.

    :param target: model the relation leads to
    :type target: type[Model]
    :param fields: referenced fields of the target
    :type fields: tuple[str, ...]
    :return: pydantic model holding only the referenced fields
    :rtype: type[BaseModel]
    """
    suffix = "".join(choices(string.ascii_uppercase, k=6)).lower()
    model_fields = target.ormar_config.model_fields
    definitions: dict[str, Any] = {
        field: (model_fields[field].__type__, None) for field in fields
    }
    return create_model(  # type: ignore[call-overload, no-any-return]
        f"PkOnly{target.get_name(lower=False)}{suffix}",
        __module__=target.__module__,
        **definitions,
    )

placeholder_key(target)

Parameters:

Name Type Description Default
target type[Model]

model a required relation leads to

required

Returns:

Type Description
dict[str, Any]

key fields of a placeholder key (-1 in every key column)

Source code in ormar/fields/key_only.py
Python
77
78
79
80
81
82
83
84
85
def placeholder_key(target: type["Model"]) -> dict[str, Any]:
    """
    :param target: model a required relation leads to
    :type target: type[Model]
    :return: key fields of a placeholder key (``-1`` in every key column)
    :rtype: dict[str, Any]
    """
    key = target.ormar_config.pk
    return key.to_dict(value=key_value(parts=(-1,) * len(key.column_aliases())))