traversal
Relation traversal without a precomputed per-class relation map.
model_dump(), save_related() and select_all(follow=True) need to know
which relations to follow so the bidirectional relations of ormar do not loop.
Instead of the cached _relation_tree() of the class they use a
PathRelationMap: one node of the relation tree, computed from the models on
the current path only, while the loaded instances are walked. Each class keeps a
bounded number of shared nodes.
The mode is selected with the ORMAR_RELATION_TRAVERSAL environment variable
(instance, the default, or the deprecated map) or with
set_relation_traversal. The map mode is removed in ormar 0.31.0.
PathRelationMap
Bases: dict
One node of the relation tree of a model, decided from the set of model
classes on the path above it: a relation is followed unless its target
model is in that set (a direct self relation is followed once) or it
points to an unresolved forward reference. It answers what the dump and
save code asks a relation map (relation in node, node[relation],
bool(node)) without building the tree, and a leaf is ... like in
the cached tree.
A node depends only on (model, models above), so nodes are shared: each
class keeps at most PATH_NODE_CACHE_SIZE of them (oldest dropped
first) and the dump of a row only looks them up::
| Text Only | |
|---|---|
1 2 3 | |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
for_path(model, ancestors)
classmethod
Returns the shared node of a model below the given set of models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[RelationMixin]
|
model class of the node |
required |
ancestors
|
frozenset
|
model classes on the path above the node |
required |
Returns:
| Type | Description |
|---|---|
PathRelationMap
|
node |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
get(relation, default=None)
Returns the child node of a relation or default when the relation
is not followed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relation
|
str
|
relation name |
required |
default
|
Any
|
value returned for a relation that is not followed |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
child node or default |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
259 260 261 262 263 264 265 266 267 268 269 270 271 | |
items()
Returns:
| Type | Description |
|---|---|
Iterator[tuple[str, Any]]
|
followed relation names with their child nodes |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
273 274 275 276 277 278 | |
store(nodes, node)
staticmethod
Puts a node into the bounded node cache of its class, dropping the oldest one when the cache is full.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
dict[frozenset, PathRelationMap]
|
node cache of the class |
required |
node
|
PathRelationMap
|
new node |
required |
Returns:
| Type | Description |
|---|---|
PathRelationMap
|
the node |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
values()
Returns:
| Type | Description |
|---|---|
Iterator[Any]
|
child nodes of the followed relations |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
280 281 282 283 284 285 | |
RelationIndex
dataclass
Followable relations of one model class: the target of every relation by
name (in name order), the relation names by target model, and the bounded
cache of the class's PathRelationMap nodes by the set of models above.
Source code in ormar/models/traversal.py
| Python | |
|---|---|
124 125 126 127 128 129 130 131 132 133 134 135 | |
RelationTraversalSettings
Holds the relation traversal mode of the process::
| Text Only | |
|---|---|
1 | |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
82 83 84 85 86 87 88 89 | |
instance_traversal_enabled()
Checks if the path based instance traversal is enabled.
Returns:
| Type | Description |
|---|---|
bool
|
True in |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
110 111 112 113 114 115 116 117 | |
relation_index(model)
Returns the followable relations of a model, leaving out relations to unresolved forward references. Kept on the class until the relation graph changes::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[RelationMixin]
|
model class |
required |
Returns:
| Type | Description |
|---|---|
RelationIndex
|
relation targets of the model |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
relation_paths(model, max_depth, reverse_depth=0, ancestors=NO_ANCESTORS)
Returns the select_related paths reachable from a model, at most
max_depth relations deep, with the same rule as PathRelationMap.
Forward foreign keys (one parent per row) are followed at every level,
reverse and many to many relations (which multiply rows) only in the
first reverse_depth levels. Only the deepest paths are listed, as
select_related loads the models along a path anyway::
| Text Only | |
|---|---|
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[RelationMixin]
|
model class to start from |
required |
max_depth
|
int
|
number of relations to follow at most |
required |
reverse_depth
|
int
|
number of first levels that follow reverse and many to many relations too |
0
|
ancestors
|
frozenset
|
model classes on the path above the model |
NO_ANCESTORS
|
Returns:
| Type | Description |
|---|---|
list[str]
|
relation paths in dunder notation |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
relation_traversal_from_environment()
Returns the relation traversal mode set in the ORMAR_RELATION_TRAVERSAL
environment variable, instance when it is not set.
Returns:
| Type | Description |
|---|---|
str
|
relation traversal mode |
Raises:
| Type | Description |
|---|---|
ValueError
|
on an unknown mode |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
67 68 69 70 71 72 73 74 75 76 77 78 79 | |
set_relation_traversal(mode)
Selects how relations are traversed by model_dump, save_related,
get_pydantic and select_all(follow=True). The map mode is
deprecated and kept for one release as a transitional switch::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
on an unknown mode |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
validate_relation_traversal(mode, stacklevel)
Checks a relation traversal mode and warns when the deprecated map
mode is selected::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
|
required |
stacklevel
|
int
|
stack level of the warning, pointing at the caller |
required |
Returns:
| Type | Description |
|---|---|
str
|
the mode |
Raises:
| Type | Description |
|---|---|
ValueError
|
on an unknown mode |
Source code in ormar/models/traversal.py
| Python | |
|---|---|
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |