utils
SliceBounds
dataclass
Normalized pagination parameters produced by :func:normalize_slice.
reverse=True signals that the ORDER BY directions should be flipped
at query build time and the fetched list reversed in memory so the
caller still sees rows in the original ordering. This is how negative
indices and negative slice bounds are emulated without a COUNT(*)
round-trip.
Attributes:
| Name | Type | Description |
|---|---|---|
limit |
Optional[int]
|
row count for |
offset |
int
|
row count for |
reverse |
bool
|
whether the query ordering must be flipped and the result list reversed in memory |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
build_flatten_map(paths)
Build a nested-Ellipsis dict from pre-split flatten path tuples — the
runtime representation threaded through model_dump recursion.
Example::
| Text Only | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
Iterable[PathParts]
|
iterable of tuple paths (each path already split on |
required |
Returns:
| Type | Description |
|---|---|
dict
|
nested dict where leaves are |
Source code in ormar/queryset/utils.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 | |
convert_set_to_required_dict(set_to_convert)
Converts set to dictionary of required keys. Required key is Ellipsis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
set_to_convert
|
set
|
set to convert to dict |
required |
Returns:
| Type | Description |
|---|---|
dict
|
set converted to dict of ellipsis |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
extract_access_chains(value)
Unwrap FieldAccessor inputs (or lists that contain them) into their
underlying dunder-path strings so downstream parsers see a uniform shape.
Anything that isn't an accessor (or list of accessors) is returned
unchanged — sets, tuples, dicts, and plain strings all pass through.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
user input for a relation-spec method ( |
required |
Returns:
| Type | Description |
|---|---|
Any
|
a dunder string, a list with each accessor replaced by its chain, or the original value unchanged |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
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 | |
get_or_create_with_retry(database, get, create)
async
Shared body of get_or_create for querysets and relation proxies.
On IntegrityError from create the get is retried once and the
error re-raised if it still finds nothing. Inside a transaction create
runs in a savepoint, serialised per transaction connection because
releasing a savepoint releases the ones opened after it; nested calls in
the same task (e.g. from a pre_save signal) reuse the held lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
DatabaseConnection
|
database the model belongs to |
required |
get
|
Callable[[], Awaitable[T]]
|
coroutine factory looking up the row |
required |
create
|
Callable[[], Awaitable[T]]
|
coroutine factory creating the row |
required |
Returns:
| Type | Description |
|---|---|
tuple[T, bool]
|
model instance and whether it was created |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 | |
get_relationship_alias_model_and_str(source_model, related_parts)
Walks the relation to retrieve the actual model on which the clause should be constructed and the alias of the table under which that model is joined.
The alias is derived from the path of the joined table, which for through models is the path of the many to many relation followed by the through model name - also when the relation itself was omitted in the related parts.
Note that through model names are removed from passed related_parts, or replaced with the many to many relation they belong to if it was omitted, so that the relation string joins the through model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related_parts
|
list
|
list of related names extracted from string |
required |
source_model
|
type[Model]
|
model from which relation starts |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, type["Model"], str, bool]
|
table prefix, target model, relation string and is through flag |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
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 | |
normalize_slice(key)
Top-level dispatcher: turns a Python integer index or slice into a
:class:SliceBounds suitable for QuerySet.
Delegates to a dedicated helper for each shape. Any shape that would
require a COUNT(*) round-trip (step != 1, a bare [:-N], or
mixed-sign bounds) raises QueryDefinitionError.
Examples::
| Text Only | |
|---|---|
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Union[int, slice]
|
integer or slice passed to |
required |
Returns:
| Type | Description |
|---|---|
SliceBounds
|
normalized slice parameters |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
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 | |
resolve_implicit_through_relation(related_parts, relation, related_field, previous_models)
Removes the through model name from related parts, or replaces it with the
name of the many to many relation the through model belongs to and returns
that name, if the relation was not part of the path
(i.e. postcategory__sort_order used from Post).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related_parts
|
list
|
split relation string, through name is removed or replaced in place |
required |
relation
|
str
|
name of the through field |
required |
related_field
|
BaseField
|
through field |
required |
previous_models
|
list[type[Model]]
|
already visited models in relation chain |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
name of the omitted many to many relation or None if it was in the path |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
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 | |
subtract_dict(current_dict, updating_dict)
Subtract one dict from another with regard for nested keys.
That way nested sets are differenced, nested dicts subtracted and keys
left empty removed. Returns a new dict and leaves current_dict (and
the dicts nested in it) unchanged, so it can be a shared cached map::
| Text Only | |
|---|---|
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_dict
|
Any
|
dict to subtract from |
required |
updating_dict
|
Any
|
dict with values to subtract |
required |
Returns:
| Type | Description |
|---|---|
dict
|
current_dict without the values of updating_dict |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
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 | |
translate_list_to_dict(list_to_trans, default=...)
Splits the list of strings by '__' and converts them to dictionary with nested models grouped by parent model. That way each model appears only once in the whole dictionary and children are grouped under parent name.
Default required key ise Ellipsis like in pydantic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_to_trans
|
Union[list, set]
|
input list |
required |
default
|
Any
|
value to use as a default value |
...
|
Returns:
| Type | Description |
|---|---|
dict
|
converted to dictionary input list |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
update(current_dict, updating_dict)
Update one dict with another but with regard for nested keys.
That way nested sets are unionised, dicts updated and only other values are overwritten.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_dict
|
Any
|
dict to update |
required |
updating_dict
|
Any
|
dict with values to update |
required |
Returns:
| Type | Description |
|---|---|
dict
|
combination of both dicts |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
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 | |
update_dict_from_list(curr_dict, list_to_update)
Converts the list into dictionary and later performs special update, where nested keys that are sets or dicts are combined and not overwritten.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
curr_dict
|
dict
|
dict to update |
required |
list_to_update
|
Union[list, set]
|
list with values to update the dict |
required |
Returns:
| Type | Description |
|---|---|
dict
|
updated dict |
Source code in ormar/queryset/utils.py
| Python | |
|---|---|
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |