mixins
Package contains functionalities divided by features. All mixins are combined into ModelTableProxy which is one of the parents of Model. The split into mixins was done to ease the maintainability of the proxy class, as it became quite complicated over time.
AliasMixin
Used to translate field names into database column names.
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
41 42 43 44 45 46 47 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 159 160 161 162 163 164 165 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 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 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 326 327 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
clear_shared_columns(target, cleared)
classmethod
Write None to a shared column when every field storing it is a
foreign key given None (e.g. two nullable foreign keys sharing a
tenant_id both set to None). The fields are counted per column,
as foreign keys stored in several columns all keep a shared column
under the same key, and a plain field storing the column is never
cleared, so its column keeps its value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
dict
|
model dict being prepared, updated in place |
required |
cleared
|
dict[SharedKey, int]
|
number of foreign keys given |
required |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
column_reader(column)
classmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
column name in the database |
required |
Returns:
| Type | Description |
|---|---|
ColumnReader
|
cached reader of the column's raw value out of an instance of
the model, its key-only model or a dict of its fields (see
|
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
fill_shared_plain_fields(kwargs)
classmethod
Fill an unset plain field storing a column foreign keys share from
the first of those foreign keys holding a value for the column, so a
new model's plain field, model_dump() and pk agree before it
is saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict[str, Any]
|
keyword arguments of a model being initialized, with relations already expanded, updated in place |
required |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
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 296 297 298 299 | |
get_column_alias(field_name)
classmethod
Returns db alias (column name in db) for given ormar field. For fields without alias field name is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
name of the field to get alias from |
required |
Returns:
| Type | Description |
|---|---|
str
|
alias (db name) if set, otherwise passed name |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
get_column_aliases(field_name)
classmethod
Returns db column names of given ormar field: one for most fields, several for a foreign key stored in several columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
name of the field |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
column names of the field |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
get_column_name_from_alias(alias)
classmethod
Returns ormar field name for given db alias (column name in db). If field do not have alias it's returned as is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str
|
|
required |
Returns:
| Type | Description |
|---|---|
str
|
field name if set, otherwise passed alias (db name) |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
get_field_name_of_column(column)
classmethod
Returns the ormar field storing a table column, also for the columns of a foreign key stored in several columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
column name in the database |
required |
Returns:
| Type | Description |
|---|---|
str
|
field name, the column itself if no field is aliased to it |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
get_field_names_of_column(column)
classmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
column name in the database |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
every ormar field storing the column, in declaration order (several when fields share it) |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
merge_relation_values(target, relation_values)
classmethod
Put the values foreign keys store (RelationLink.values_for) into a
model dict about to be written. The foreign keys' own entries are
dropped from the dict first, then their values are merged in the
order the fields are declared, so the result never depends on the
order the foreign keys were processed in.
A column several fields share keeps one value: a None is not
written to it while another of its fields holds a value (a shared
column is cleared only when every foreign key storing it is cleared
and no plain field stores it), an unset plain field sharing the
column is filled in, and two different values are an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
dict
|
model dict being prepared, updated in place |
required |
relation_values
|
dict[str, dict[str, Any]]
|
values of each foreign key by key, by foreign key name |
required |
Raises:
| Type | Description |
|---|---|
ModelPersistenceError
|
if a shared column gets two values |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
reset_column_caches()
classmethod
Drop the cached column mappings of the model and the layout of its primary key, e.g. after forward references are resolved and a foreign key's columns are known.
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
shared_value_keys()
classmethod
Returns:
| Type | Description |
|---|---|
dict[str, SharedKey]
|
for the keys of columns several fields share, the key the
column's value is kept under and the column (see
|
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
256 257 258 259 260 261 262 263 264 265 266 267 268 | |
translate_aliases_to_columns(new_kwargs)
classmethod
Translates dictionary of model fields changing aliases into field names. If field has no alias the alias is already a field name. Only fields present in the dictionary are translated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dict with aliases and their values |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dict with fields names and their values |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
translate_columns_to_aliases(new_kwargs)
classmethod
Translates dictionary of model fields changing field names into aliases. If field has no alias the field name remains intact. Only fields present in the dictionary are translated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dict with fields names and their values |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dict with aliases and their values |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
translate_row_to_fields(row)
classmethod
Translates a database row of the model's table into field values: the
columns of a foreign key stored in several columns become one tuple
(None when any of them is null).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
dict
|
column values by column name |
required |
Returns:
| Type | Description |
|---|---|
dict
|
field values by field name |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
value_keys(field_name)
classmethod
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
name of a field stored in the model's table |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
keys the field's values are kept under in a model dict about to be written: its name, or its columns for a foreign key stored in several columns; a shared column under the key it is kept under |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
ExcludableMixin
Bases: RelationMixin
Used to include/exclude given set of fields on models during load and dict() calls.
Source code in ormar/models/mixins/excludable_mixin.py
| Python | |
|---|---|
21 22 23 24 25 26 27 28 29 30 31 32 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 159 160 161 162 163 164 165 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
get_names_to_exclude(excludable, alias)
classmethod
Returns a set of models field names that should be explicitly excluded during model initialization.
Those fields will be set to None to avoid ormar/pydantic setting default values on them. They should be returned as None in any case.
Used in parsing data from database rows that construct Models by initializing them with dicts constructed from those db rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alias
|
str
|
alias of current relation |
required |
excludable
|
ExcludableItems
|
structure of fields to include and exclude |
required |
Returns:
| Type | Description |
|---|---|
set
|
set of field names that should be excluded |
Source code in ormar/models/mixins/excludable_mixin.py
| Python | |
|---|---|
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 | |
own_table_columns(model, excludable, alias='', use_alias=False, add_pk_columns=True)
classmethod
Returns list of aliases or field names for given model. Aliases/names switch is use_alias flag.
If provided only fields included in fields will be returned. If provided fields in exclude_fields will be excluded in return.
Primary key field is always added and cannot be excluded (will be added anyway).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
add_pk_columns
|
bool
|
flag if add primary key - always yes if ormar parses data |
True
|
alias
|
str
|
relation prefix |
''
|
excludable
|
ExcludableItems
|
structure of fields to include and exclude |
required |
model
|
Union[type[Model], type[ModelRow]]
|
model on columns are selected |
required |
use_alias
|
bool
|
flag if aliases or field names should be used |
False
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list of column field names or aliases |
Source code in ormar/models/mixins/excludable_mixin.py
| Python | |
|---|---|
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 | |
MergeModelMixin
Used to collect models instances returned by database, but already initialized to ormar Models.
Rows of one model returned by a join are materialized into a single shared
instance (see RowRegistry), so the same main model can repeat in the
list of materialized rows; in the end all main models should be unique.
Source code in ormar/models/mixins/merge_mixin.py
| Python | |
|---|---|
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
merge_instances_list(result_rows, *, excludable)
classmethod
Collapses a list of materialized rows into a list of unique models, keeping the order of first occurrence.
Each top-level model also has the queryset's ExcludableItems
attached via the __ormar_excludable__ slot so a bare model_dump()
on the result can resolve the (lazily cached) flatten map without going
back through the queryset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_rows
|
list[Model]
|
list of models materialized from consecutive rows, where rows of the same main model resolve to the same instance |
required |
excludable
|
ExcludableItems
|
ExcludableItems to attach to every top-level
instance via the |
required |
Returns:
| Type | Description |
|---|---|
list["Model"]
|
list of unique main models |
Source code in ormar/models/mixins/merge_mixin.py
| Python | |
|---|---|
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
PydanticMixin
Bases: RelationMixin
Source code in ormar/models/mixins/pydantic_mixin.py
| Python | |
|---|---|
29 30 31 32 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 159 160 161 162 163 164 165 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 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 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 326 327 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 355 356 357 358 359 360 361 362 | |
copy_selected_validators_type(model, fields, validator_type)
classmethod
Copy field validators from ormar model to generated pydantic model.
Source code in ormar/models/mixins/pydantic_mixin.py
| Python | |
|---|---|
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
get_pydantic(*, include=None, exclude=None, flatten_fields=None, flatten_all=False, max_depth=None, omit_back_relations=None)
classmethod
Returns a pydantic model out of ormar model.
Converts also nested ormar models into pydantic models. Each related
model is generated once as a named nested model and relations point to
it, so cyclic relations give recursive models ($ref in OpenAPI).
Relation fields of nested models are optional, the root model keeps
the required/optional rules of its own fields. Generated models are
cached and their names do not depend on the order of calls
(Book, Book_nested, Book_depth2, a selector hash suffix).
Can be used to fully exclude certain fields in fastapi response and requests.
Relations listed in flatten_fields (or all relations with
flatten_all) are represented by the related model's primary key
(or a list of primary keys for many-to-many and reverse relations)
instead of a nested pydantic model, matching the output of
model_dump called with the same arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include
|
Union[set, dict, None]
|
fields of own and nested models to include |
None
|
exclude
|
Union[set, dict, None]
|
fields of own and nested models to exclude |
None
|
flatten_fields
|
Union[set, list, str, tuple, dict, FieldAccessor, None]
|
relations to represent as their primary key |
None
|
flatten_all
|
bool
|
if True every relation is represented as its pk |
False
|
max_depth
|
Optional[int]
|
number of relation levels to nest at most, None for no limit (cycles become recursive models) |
None
|
omit_back_relations
|
Optional[bool]
|
leave the relation back to the parent out of nested models (one nested model per (model, relation) pair), False by default (one nested model per model) |
None
|
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
on invalid flatten paths or when a flattened relation has sub-field include/exclude |
Source code in ormar/models/mixins/pydantic_mixin.py
| Python | |
|---|---|
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
SavePrepareMixin
Bases: RelationMixin, AliasMixin
Used to prepare models to be saved in database
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
21 22 23 24 25 26 27 28 29 30 31 32 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 159 160 161 162 163 164 165 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 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 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 326 327 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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
dump_all_json_fields_to_str(model_dict)
classmethod
Receives dictionary of model that is about to be saved and changes all json fields into strings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_dict
|
dict
|
dictionary of model that is about to be saved |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dictionary of model that is about to be saved |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
populate_default_values(new_kwargs)
classmethod
Receives dictionary of model that is about to be saved and populates the default value on the fields that have the default value set, but no actual value was passed by the user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dictionary of model that is about to be saved |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dictionary of model that is about to be saved |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
populate_onupdate_value(new_kwargs, explicit_fields=None)
classmethod
Populates on_update values for fields that have on_update configured and were not provided explicitly.
If explicit_fields is omitted the keys of new_kwargs are
treated as explicitly provided. Callers pass the set explicitly when
new_kwargs is a full model_dump (e.g. bulk_update) so that
fields the user actually mutated can be distinguished from values that
are just present by virtue of the dump.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dictionary of values about to be used for the update |
required |
explicit_fields
|
Optional[set[str]]
|
fields that should be considered user-provided |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
new_kwargs with on_update values populated |
Source code in ormar/models/mixins/save_mixin.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 | |
prepare_model_to_save(new_kwargs)
classmethod
Combines all preparation methods before saving. Removes primary key for if it's nullable or autoincrement pk field, and it's set to None. Substitute related models with their primary key values as fk column. Populates the default values for field with default set and no value. Translate columns into aliases (db names).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dictionary of model that is about to be saved |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dictionary of model that is about to be saved |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
prepare_model_to_update(new_kwargs)
classmethod
Combines all preparation methods before updating.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dictionary of model that is about to be saved |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dictionary of model that is about to be updated |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
58 59 60 61 62 63 64 65 66 67 68 69 70 | |
reconvert_str_to_bytes(model_dict)
classmethod
Receives dictionary of model that is about to be saved and changes all bytes fields that are represented as strings back into bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_dict
|
dict
|
dictionary of model that is about to be saved |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dictionary of model that is about to be saved |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
restore_nested_models_json_fields(decoded)
staticmethod
Restores decoded JSON field values of related models serialized by
serialize_nested_models_json_fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decoded
|
list[tuple[Model, dict[str, Any]]]
|
related models with their decoded JSON field values |
required |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
261 262 263 264 265 266 267 268 269 270 271 272 273 | |
serialize_nested_models_json_fields(kwargs)
classmethod
Serializes JSON fields of related model instances passed in kwargs to strings, as pydantic validation of the model validates them again against their JSON fields, which only accept strings.
The related models keep the serialized values only for the validation:
the decoded values are returned, so they can be restored with
restore_nested_models_json_fields afterwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
dict
|
dictionary of keyword arguments |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Model, dict[str, Any]]]
|
related models with their decoded JSON field values |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
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 | |
substitute_models_with_pks(model_dict)
classmethod
Receives dictionary of model that is about to be saved and changes all related models that are stored as foreign keys to their fk value.
The values of all foreign keys are collected first and merged into the
dictionary afterwards (see merge_relation_values), so columns the
foreign keys share get the same result whatever order they come in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_dict
|
dict
|
dictionary of model that is about to be saved |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dictionary of model that is about to be saved |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
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 | |
validate_update_values(new_kwargs)
classmethod
Receives dictionary of values about to be set by a queryset update, validates them and converts them into column values, the same way initializing a model would:
- foreign keys accept the related model, a dictionary with its pk or the pk itself and are replaced with the validated pk value
- json fields accept a json string or a value to be dumped and are replaced with the parsed value
- other fields are validated with their own validators
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_kwargs
|
dict
|
dictionary of values about to be used for the update |
required |
Returns:
| Type | Description |
|---|---|
dict
|
dictionary of column values about to be used for the update |
Source code in ormar/models/mixins/save_mixin.py
| Python | |
|---|---|
326 327 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 355 356 357 358 359 360 | |