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
71 72 73 74 75 76 77 78 79 80 81 |
|
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
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
register_many_to_many_relation_on_build(field)
Registers connection between through model and both sides of the m2m relation. Registration include also reverse relation side to be able to join both sides.
Relation is registered by model name and relation field name to allow for multiple relations between two Models that needs to have different aliases for proper sql joins.
By default relation name is a model.name.lower().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
ManyToManyField
|
relation field |
required |
Source code in ormar\models\helpers\relations.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
register_relation_in_alias_manager(field)
Registers the relation (and reverse relation) in alias manager. The m2m relations require registration of through model between actual end models of the relation.
Delegates the actual registration to: m2m - register_many_to_many_relation_on_build fk - register_relation_on_build
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
ForeignKeyField
|
relation field |
required |
Source code in ormar\models\helpers\relations.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
register_relation_on_build(field)
Registers ForeignKey relation in alias_manager to set a table_prefix. Registration include also reverse relation side to be able to join both sides.
Relation is registered by model name and relation field name to allow for multiple relations between two Models that needs to have different aliases for proper sql joins.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
ForeignKeyField
|
relation field |
required |
Source code in ormar\models\helpers\relations.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
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
101 102 103 104 105 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 |
|
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
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 |
|
replace_models_with_copy(annotation, source_model_field=None)
Replaces all models in annotation with their copies to avoid circular references.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
annotation |
Type
|
annotation to replace models in |
required |
Returns:
Type | Description |
---|---|
Type
|
annotation with replaced models |
Source code in ormar\models\helpers\relations.py
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 |
|
reverse_field_not_already_registered(model_field)
Checks if child is already registered in parents pydantic fields.
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
328 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 |
|
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.
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
292 293 294 295 296 297 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 |
|