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 | |
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 | |
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 | |
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 ( |
Source code in ormar/fields/key_only.py
| Python | |
|---|---|
77 78 79 80 81 82 83 84 85 | |