relations
expand_reverse_relationship(model_field)
If the reverse relation has not been set before it's set here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ForeignKeyField
|
|
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
35 36 37 38 39 40 41 42 43 44 45 | |
expand_reverse_relationships(model)
Iterates through model_fields of given model and verifies if all reverse relation have been populated on related models.
If the reverse relation has not been set before it's set here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model on which relation should be checked and registered |
required |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
register_reverse_model_fields(model_field)
Registers reverse ForeignKey field on related model. By default it's name.lower()+'s' of the model on which relation is defined.
But if the related_model name is provided it's registered with that name. Autogenerated reverse fields also set related_name to the original field name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ForeignKeyField
|
original relation ForeignKey field |
required |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
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 | |
register_through_shortcut_fields(model_field)
Registers m2m relation through shortcut on both ends of the relation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ManyToManyField
|
relation field defined in parent model |
required |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
reset_relation_caches(model)
Drop the lazily cached relation metadata of a model that gains a reverse relation, so models already used before a related model was defined see the new relation (and lose trusted row construction). The cached relation paths of every model connected to it are rebuilt on next use too.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[Model]
|
model receiving a reverse relation |
required |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
resolve_through_fields(model_field)
Adopt the foreign keys a user-defined through model already declares to the
owner and the target of a many-to-many relation (e.g. a legacy link table
keyed by them), so ormar uses those fields instead of adding its own.
Explicit through_relation_name / through_reverse_relation_name must
name foreign keys to the owner / target when the through model declares
them; several foreign keys to one side, and any self-referencing link
table, need those names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ManyToManyField
|
many-to-many relation field |
required |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if the through model has relations to other models or unresolved forward references, if an explicit name points at the wrong field, or if the foreign keys are ambiguous |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 | |
resolve_through_side(model_field, relations, attribute, side)
Validate or pick the through model foreign key leading to one side of a many-to-many relation and store its name on the relation field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ManyToManyField
|
many-to-many relation field |
required |
relations
|
dict[str, BaseField]
|
non-virtual relation fields of the through model by name |
required |
attribute
|
str
|
|
required |
side
|
type[Model]
|
model the foreign key must lead to |
required |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if an explicit name is not a foreign key to the side, or several foreign keys lead to it without an explicit name |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
166 167 168 169 170 171 172 173 174 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 | |
reverse_field_annotation(kind, annotation)
Pydantic annotation of a reverse relation field: a reverse foreign key accepts one model or a list of them, a reverse one-to-one a single model, and a many-to-many keeps the annotation of its field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
RelationKind
|
kind of the reverse relation field |
required |
annotation
|
Any
|
annotation of the reverse field |
required |
Returns:
| Type | Description |
|---|---|
Any
|
annotation to register on the related model |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
reverse_field_not_already_registered(model_field)
Checks if the reverse side of the relation is not yet registered on the related model, i.e. no field named as the relation's related_name exists there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ForeignKeyField
|
original relation ForeignKey field |
required |
Returns:
| Type | Description |
|---|---|
bool
|
result of the check |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if related name is already used but lead to different related model |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
verify_deferred_related_names(model_field)
Repeats the related_name checks deferred while given field was leading to an unresolved ForwardRef, now that its target model is known.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_field
|
ForeignKeyField
|
just resolved relation field |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if the field's name was used as related_name by a relation leading from a different model |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
verify_related_name_dont_duplicate(related_name, model_field)
Verifies whether the used related_name (regardless of the fact if user defined or auto generated) is already used on related model, but is connected with other model than the one that we connect right now.
A field still leading to an unresolved ForwardRef cannot be compared yet, so the
check is deferred and repeated by verify_deferred_related_names once the
field is resolved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related_name
|
str
|
|
required |
model_field
|
ForeignKeyField
|
original relation ForeignKey field |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
ModelDefinitionError
|
if name is already used but lead to different related model |
Source code in ormar/models/helpers/relations.py
| Python | |
|---|---|
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |