alias_mixin
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 | |
SharedKey
Bases: NamedTuple
Where the value of a column several fields share is kept in a model dict.
Attributes:
| Name | Type | Description |
|---|---|---|
kept |
str
|
key the column's value is kept under (the plain field's name when one stores it, else the first field's key) |
column |
str
|
the shared column |
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
13 14 15 16 17 18 19 20 21 22 23 | |
SharedPlainField
Bases: NamedTuple
A plain field storing a column foreign keys share, and where those foreign keys hold the column's value.
Attributes:
| Name | Type | Description |
|---|---|---|
field |
str
|
name of the plain field |
sources |
tuple[tuple[str, str], ...]
|
|
Source code in ormar/models/mixins/alias_mixin.py
| Python | |
|---|---|
26 27 28 29 30 31 32 33 34 35 36 37 38 | |