excludable
Excludable
dataclass
Class that keeps sets of fields to include, exclude, and flatten for a single model at a given alias/relation level.
Source code in ormar/models/excludable.py
| Python | |
|---|---|
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 | |
get_copy()
Return copy of self to avoid in place modifications.
:return: copy of self with copied sets :rtype: ormar.models.excludable.Excludable
Source code in ormar/models/excludable.py
| Python | |
|---|---|
250 251 252 253 254 255 256 257 258 259 260 261 | |
is_excluded(key)
Check if field in excluded (in set or set is {...}).
:param key: key to check :type key: str :return: result of the check :rtype: bool
Source code in ormar/models/excludable.py
| Python | |
|---|---|
287 288 289 290 291 292 293 294 295 296 | |
is_flattened(key)
Check if relation is flattened (in set or set is {...}).
:param key: relation name to check :type key: str :return: result of the check :rtype: bool
Source code in ormar/models/excludable.py
| Python | |
|---|---|
298 299 300 301 302 303 304 305 306 307 | |
is_included(key)
Check if field in included (in set or set is {...}).
:param key: key to check :type key: str :return: result of the check :rtype: bool
Source code in ormar/models/excludable.py
| Python | |
|---|---|
276 277 278 279 280 281 282 283 284 285 | |
set_values(value, slot)
Appends the data to the chosen slot (include/exclude/flatten).
:param value: set of values to add :type value: set :param slot: which set to add the values to :type slot: Slot
Source code in ormar/models/excludable.py
| Python | |
|---|---|
263 264 265 266 267 268 269 270 271 272 273 274 | |
ExcludableItems
Keeps a dictionary of Excludables by alias + model_name keys to allow quick lookup by nested models without need to travers deeply nested dictionaries and passing include/exclude around.
Source code in ormar/models/excludable.py
| Python | |
|---|---|
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 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
build(items, model_cls, slot='include')
Receives the one of the types of items and parses them as to achieve a end situation with one excludable per alias/model in relation.
Each excludable has three sets of values - include, exclude, and flatten.
:param items: values to be included, excluded or flattened :type items: Union[list[str], str, tuple[str], set[str], dict] :param model_cls: source model from which relations are constructed :type model_cls: ormar.models.metaclass.ModelMetaclass :param slot: which slot to write parsed values into :type slot: Slot
Source code in ormar/models/excludable.py
| Python | |
|---|---|
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 | |
flatten_map()
Return a :class:FlattenMap over the stored flatten paths, built
lazily on first call and cached for reuse. The cache is invalidated
whenever _set_slot adds a new flatten path. Returns None when
no flatten paths are stored, so callers can short-circuit.
:return: FlattenMap wrapping the nested-Ellipsis dict, or None
when no flatten paths are stored
:rtype: Optional[FlattenMap]
Source code in ormar/models/excludable.py
| Python | |
|---|---|
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
from_excludable(other)
classmethod
Copy passed ExcludableItems to avoid inplace modifications.
:param other: other excludable items to be copied :type other: ormar.models.excludable.ExcludableItems :return: copy of other :rtype: ormar.models.excludable.ExcludableItems
Source code in ormar/models/excludable.py
| Python | |
|---|---|
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
get(model_cls, alias='')
Return Excludable for given model and alias.
:param model_cls: target model to check :type model_cls: ormar.models.metaclass.ModelMetaclass :param alias: table alias from relation manager :type alias: str :return: Excludable for given model and alias :rtype: ormar.models.excludable.Excludable
Source code in ormar/models/excludable.py
| Python | |
|---|---|
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
include_entry_count()
Returns count of include items inside.
Source code in ormar/models/excludable.py
| Python | |
|---|---|
338 339 340 341 342 343 344 345 | |
validate_flatten_vs_excludable(source_model)
Ensure no flattened relation has sub-field include/exclude on its target. Whole-relation include/exclude at the parent level is allowed; only sub-field selection on the flattened child is flagged.
:param source_model: model from which flatten paths are rooted :type source_model: type[Model] :raises QueryDefinitionError: when a flattened relation has sub-field include/exclude on its target model
Source code in ormar/models/excludable.py
| Python | |
|---|---|
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 | |
FlattenMap
Nested-dict view of flatten directives with Ellipsis at leaves. Wraps the
Optional[dict] shape produced by :class:ExcludableItems so the few
operations that consume it (leaf check, descent, validation against
include/exclude) live alongside the data.
A truthy instance means at least one relation will be rendered as its primary key.
Source code in ormar/models/excludable.py
| Python | |
|---|---|
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 | |
data
property
Underlying nested-Ellipsis dict, or None when only flatten_all
drives behavior. Exposed primarily for inspection and tests.
:return: stored nested-Ellipsis dict, or None :rtype: Optional[dict]
check_vs_selector(selector, selector_kind, path='')
Walk the flatten map alongside an include/exclude dict and raise when a flattened leaf has sub-field selection on its target.
flatten_all alone never produces a conflict because there is no
explicit per-relation map to compare against.
:param selector: current level of include / exclude nested dict :type selector: Union[set, dict, None] :param selector_kind: "include" or "exclude" - used in error message :type selector_kind: str :param path: dunder path accumulator for the error message :type path: str
Source code in ormar/models/excludable.py
| Python | |
|---|---|
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
descend(field)
Return the flatten sub-map for the nested model reached via field,
or None when there is no sub-map. Callers must check
is_field_flattened first — flatten_all instances short-circuit
there before reaching descend.
:param field: relation name to descend into :type field: str :return: nested FlattenMap or None :rtype: Optional[FlattenMap]
Source code in ormar/models/excludable.py
| Python | |
|---|---|
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
is_field_flattened(field)
Decide if a relation field should be rendered as its primary key.
flatten_all wins globally; otherwise an Ellipsis leaf at field
triggers flattening.
:param field: name of the relation field :type field: str :return: True if the field should be rendered as a pk value :rtype: bool
Source code in ormar/models/excludable.py
| Python | |
|---|---|
132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
convert_all(items)
Convert pydantic __all__ special index into the ormar form, which does
not support index-based exclusions.
:param items: current include/exclude value
:type items: Union[set, dict, None]
:return: items with __all__ unwrapped if present
:rtype: Union[set, dict, None]
Source code in ormar/models/excludable.py
| Python | |
|---|---|
42 43 44 45 46 47 48 49 50 51 52 53 54 | |
filter_not_excluded_fields(fields, include, exclude)
Apply include / exclude dicts to a flat field list, returning the field
names that survive filtering. Mirrors the rules used during model_dump
serialization.
:param fields: candidate field names :type fields: Union[list, set] :param include: fields to include :type include: Optional[dict] :param exclude: fields to exclude :type exclude: Optional[dict] :return: list of field names that pass the include/exclude filter :rtype: list
Source code in ormar/models/excludable.py
| Python | |
|---|---|
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 | |
join_path(parts, tail=None)
Build a user-facing dunder path string from a pre-split tuple, optionally appending one extra segment. Used only for error messages — the rest of the module keeps paths as tuples.
:param parts: pre-split path segments :type parts: PathParts :param tail: optional additional segment to append :type tail: Optional[str] :return: dunder-joined path string :rtype: str
Source code in ormar/models/excludable.py
| Python | |
|---|---|
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
normalize_to_dict(items)
Convert a set form of include/exclude into its dict equivalent, leaving
dicts and None unchanged.
:param items: include/exclude value in any user-accepted form
:type items: Union[set, dict, None]
:return: dict form of items, or None
:rtype: Optional[dict]
Source code in ormar/models/excludable.py
| Python | |
|---|---|
57 58 59 60 61 62 63 64 65 66 67 | |
skip_ellipsis(items, key, default=None)
Descend one level into an include/exclude dict by key, returning
default when the lookup yields Ellipsis. Ellipsis at this position
means "all fields below" rather than a concrete set/dict to recurse into.
:param items: current include/exclude value :type items: Union[set, dict, None] :param key: key for nested relations to check :type key: str :param default: value returned when the lookup yields Ellipsis :type default: Any :return: nested value of the items :rtype: Union[set, dict, None]
Source code in ormar/models/excludable.py
| Python | |
|---|---|
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
walk_flatten_vs_selector(flatten_map, selector, selector_kind, path)
Recursive worker behind :meth:FlattenMap.check_vs_selector — kept at
module level so the recursion stays on a plain dict and does not allocate
intermediate :class:FlattenMap instances.
:param flatten_map: current level of the flatten nested dict :type flatten_map: dict :param selector: current level of include / exclude nested dict :type selector: Union[set, dict, None] :param selector_kind: "include" or "exclude" - used in error message :type selector_kind: str :param path: dunder path accumulator for the error message :type path: str
Source code in ormar/models/excludable.py
| Python | |
|---|---|
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 | |