querysetproxy
QuerysetProxy
Bases: Generic[T]
Exposes QuerySet methods on relations, but also handles creating and removing of through Models for m2m relations.
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
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 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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 | |
queryset
property
writable
Returns queryset if it's set, AttributeError otherwise.
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
__getitem__(key)
Slices the underlying QuerySet using Python integer or slice syntax.
Negative indices and slices are supported the same way as on
QuerySet.__getitem__ — they are translated into a reversed-order
query plus an in-memory list reversal on .all().
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Union[int, slice]
|
integer index or slice |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy with updated pagination |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 | |
all(*args, **kwargs)
async
Returns all rows from a database for given model for set filter options.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).all().
If there are no rows meeting the criteria an empty list is returned.
Actual call delegated to QuerySet.
List of related models is cleared before the call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
list[Model]
|
list of returned models |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
avg(columns)
async
Returns avg value of columns for rows matching the given criteria
(applied with filter and exclude if set before).
Returns:
| Type | Description |
|---|---|
Union[int, float, list]
|
avg value of columns |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
273 274 275 276 277 278 279 280 281 | |
check_update_filtered(each)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
each
|
bool
|
flag if all related models should be affected if no filter is passed |
required |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if the queryset has no filter besides the
one on the parent model and |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |
clear(keep_reversed=True)
async
Removes all related models from given relation.
Removes all through models for m2m relation.
For reverse FK relations keep_reversed flag marks if the reversed models should be kept or deleted from the database too (False means that models will be deleted, and not only removed from relation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keep_reversed
|
bool
|
flag if reverse models in reverse FK should be deleted or not, keep_reversed=False deletes them from database. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
number of deleted models |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
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 | |
count(distinct=True)
async
Returns number of rows matching the given criteria
(applied with filter and exclude if set before).
If distinct is True (the default), this will return
the number of primary rows selected. If False,
the count will be the total number of rows returned
(including extra rows for one-to-many or many-to-many
left select_related table joins).
False is the legacy (buggy) behavior for workflows that depend on it.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distinct
|
bool
|
flag if the primary table rows should be distinct or not |
True
|
Returns:
| Type | Description |
|---|---|
int
|
number of rows |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | |
create(*, on_conflict=None, conflict_target=None, update_fields=None, update_values=None, **kwargs)
async
create(
*, on_conflict: Literal["ignore"], **kwargs: Any
) -> Optional[T]
create(
*,
on_conflict: Literal["update"],
conflict_target: Optional[list[str]] = None,
update_fields: Optional[list[str]] = None,
update_values: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> T
create(*, on_conflict: None = None, **kwargs: Any) -> T
Creates the model instance, saves it in a database and returns the updates model (with pk populated if not passed and autoincrement is set).
The allowed kwargs are Model fields names and proper value types.
For m2m relation the through model is created automatically.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on_conflict
|
Optional[Literal['ignore', 'update']]
|
|
None
|
conflict_target
|
Optional[list[str]]
|
names of the fields of the unique key the row
conflicts on, only with |
None
|
update_fields
|
Optional[list[str]]
|
names of the fields updated from the inserted
row, only with |
None
|
update_values
|
Optional[dict[str, Any]]
|
values or expressions set on conflict by field
name, only with |
None
|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Model]
|
created model, |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
for |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
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 | |
create_through_instance(child, **kwargs)
async
Crete a through model instance in the database for m2m relations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
dict of additional keyword arguments for through instance |
{}
|
child
|
T
|
child model instance |
required |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
delete_through_instance(child)
async
Removes through model instance from the database for m2m relations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
T
|
child model instance |
required |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
198 199 200 201 202 203 204 205 206 207 208 209 210 | |
exclude(*args, **kwargs)
Works exactly the same as filter and all modifiers (suffixes) are the same, but returns a not condition.
So if you use filter(name='John') which is where name = 'John' in SQL,
the exclude(name='John') equals to where name <> 'John'
Note that all conditions are joined so if you pass multiple values it becomes a union of conditions.
exclude(name='John', age>=35) will become
where not (name='John' and age>=35)
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
filtered QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 | |
exclude_fields(columns)
With exclude_fields() you can select subset of model columns that will
be excluded to limit the data load.
It's the opposite of fields() method so check documentation above
to see what options are available.
Especially check above how you can pass also nested dictionaries and sets as a mask to exclude fields from whole hierarchy.
Note that fields() and exclude_fields() works both for main models
(on normal queries like get, all etc.)
as well as select_related and prefetch_related models
(with nested notation).
Mandatory fields cannot be excluded as it will raise ValidationError,
to exclude a field it has to be nullable.
Pk column cannot be excluded - it's always auto added even if explicitly excluded.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Union[list, str, set, dict]
|
columns to exclude |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 | |
exists()
async
Returns a bool value to confirm if there are rows matching the given criteria
(applied with filter and exclude if set).
Actual call delegated to QuerySet.
Returns:
| Type | Description |
|---|---|
bool
|
result of the check |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
212 213 214 215 216 217 218 219 220 221 222 | |
fields(columns)
With fields() you can select subset of model columns to limit the data load.
Note that fields() and exclude_fields() works both for main models
(on normal queries like get, all etc.)
as well as select_related and prefetch_related
models (with nested notation).
You can select specified fields by passing a str, list[str], set[str] or
dict with nested definition.
To include related models use notation
{related_name}__{column}[__{optional_next} etc.].
fields() can be called several times, building up the columns to select.
If you include related models into select_related() call but you won't specify
columns for those models in fields - implies a list of all fields for
those nested models.
Mandatory fields cannot be excluded as it will raise ValidationError,
to exclude a field it has to be nullable.
Pk column cannot be excluded - it's always auto added even if not explicitly included.
You can also pass fields to include as dictionary or set.
To mark a field as included in a dictionary use it's name as key and ellipsis as value.
To traverse nested models use nested dictionaries.
To include fields at last level instead of nested dictionary a set can be used.
To include whole nested model specify model related field name and ellipsis.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Union[list, str, set, dict]
|
columns to include |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 | |
filter(*args, **kwargs)
Allows you to filter by any Model attribute/field
as well as to fetch instances, with a filter across an FK relationship.
You can use special filter suffix to change the filter operands:
- exact - like
album__name__exact='Malibu'(exact match) - iexact - like
album__name__iexact='malibu'(exact match case insensitive) - contains - like
album__name__contains='Mal'(sql like) - icontains - like
album__name__icontains='mal'(sql like case insensitive) - in - like
album__name__in=['Malibu', 'Barclay'](sql in) - isnull - like
album__name__isnull=True(sql is null) (isnotnullalbum__name__isnull=False(sql is not null)) - gt - like
position__gt=3(sql >) - gte - like
position__gte=3(sql >=) - lt - like
position__lt=3(sql <) - lte - like
position__lte=3(sql <=) - startswith - like
album__name__startswith='Mal'(exact start match) - istartswith - like
album__name__istartswith='mal'(case insensitive) - endswith - like
album__name__endswith='ibu'(exact end match) - iendswith - like
album__name__iendswith='IBU'(case insensitive)
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
filtered QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 | |
first(*args, **kwargs)
async
Gets the first row from the db ordered by primary key column ascending.
Actual call delegated to :meth:QuerySet.first, which always issues
LIMIT 1 and therefore never raises MultipleMatches. Use
:meth:get instead if you want to assert a single match.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).first().
List of related models is cleared before the call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Model
|
returned model |
Raises:
| Type | Description |
|---|---|
NoMatch
|
if no rows match the criteria |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
first_or_none(*args, **kwargs)
async
Gets the first row from the db ordered by primary key column ascending.
Behaves like :meth:first but returns None instead of raising
NoMatch when no row matches.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).first_or_none().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Model]
|
returned model or None |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
get(*args, **kwargs)
async
Gets a single row from the db matching the criteria set by args/kwargs.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).get(). Actual call is delegated to
:meth:QuerySet.get; see its docstring for the full contract.
When criteria are set (here or via chained :meth:filter/:meth:exclude)
the query asserts that exactly one row matches: NoMatch is raised
when none match and MultipleMatches when more than one matches.
When no criteria are set, get() returns the last row of the table
ordered by primary key descending.
List of related models is cleared before the call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Model
|
returned model |
Raises:
| Type | Description |
|---|---|
NoMatch
|
if no rows match the criteria |
MultipleMatches
|
when criteria are set and more than one row matches them. |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
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 | |
get_or_create(_defaults=None, *args, **kwargs)
async
Combination of create and get methods.
Tries to get a row meeting the criteria for kwargs
and if NoMatch exception is raised
it creates a new one with given kwargs and _defaults.
If a concurrent caller wins the race and creates a matching row
between this call's get and create, the create raises
sqlalchemy.exc.IntegrityError and get is retried once.
If the retry still finds no row, the original IntegrityError is
re-raised because the violation isn't a race but a legitimate
constraint conflict (e.g. a different unique key). Inside a transaction
the create runs in a savepoint, so the transaction stays usable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
_defaults
|
Optional[dict[str, Any]]
|
default values for creating object |
None
|
Returns:
| Type | Description |
|---|---|
tuple("T", bool)
|
model instance and a boolean |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 | |
get_or_none(*args, **kwargs)
async
Gets a single row from the db matching the criteria set by args/kwargs.
Behaves like :meth:get but returns None instead of raising
NoMatch when no row matches. MultipleMatches is still raised
when criteria are provided and more than one row matches.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).get_or_none().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Model]
|
returned model or None |
Raises:
| Type | Description |
|---|---|
MultipleMatches
|
when criteria are set (via |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
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 | |
iterate(*args, **kwargs)
async
Return async iterable generator for all rows from a database for given model.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).iterate().
If there are no rows meeting the criteria an empty async generator is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
AsyncGenerator[Model]
|
asynchronous iterable generator of returned models |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
last(*args, **kwargs)
async
Gets the last row from the db ordered by primary key column descending.
Actual call delegated to :meth:QuerySet.last, which always issues
LIMIT 1 and therefore never raises MultipleMatches. Use
:meth:get instead if you want to assert a single match.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).last().
List of related models is cleared before the call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Model
|
returned model |
Raises:
| Type | Description |
|---|---|
NoMatch
|
if no rows match the criteria |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
last_or_none(*args, **kwargs)
async
Gets the last row from the db ordered by primary key column descending.
Behaves like :meth:last but returns None instead of raising
NoMatch when no row matches.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).last_or_none().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Model]
|
returned model or None |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
limit(limit_count)
You can limit the results to desired number of parent models.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit_count
|
int
|
number of models to limit |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 | |
max(columns)
async
Returns max value of columns for rows matching the given criteria
(applied with filter and exclude if set before).
Returns:
| Type | Description |
|---|---|
Any
|
max value of column(s) |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
243 244 245 246 247 248 249 250 251 | |
min(columns)
async
Returns min value of columns for rows matching the given criteria
(applied with filter and exclude if set before).
Returns:
| Type | Description |
|---|---|
Any
|
min value of column(s) |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
253 254 255 256 257 258 259 260 261 | |
offset(offset)
You can also offset the results by desired number of main models.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
numbers of models to offset |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 | |
order_by(columns)
With order_by() you can order the results from database based on your
choice of fields.
You can provide a string with field name or list of strings with fields names.
Ordering in sql will be applied in order of names you provide in order_by.
By default if you do not provide ordering ormar explicitly orders by
all primary keys
If you are sorting by nested models that causes that the result rows are
unsorted by the main model ormar will combine those children rows into
one main model.
The main model will never duplicate in the result
To order by main model field just provide a field name
To sort on nested models separate field names with dunder '__'.
You can sort this way across all relation types -> ForeignKey,
reverse virtual FK and ManyToMany fields.
To sort in descending order provide a hyphen in front of the field name
To sort randomly provide "?", ormar.random() or ormar.Random()
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Union[list, str, OrderAction, RandomOrder, Random]
|
columns by which models should be sorted |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 | |
paginate(page, page_size=20)
You can paginate the result which is a combination of offset and limit clauses. Limit is set to page size and offset is set to (page-1) * page_size.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page_size
|
int
|
numbers of items per page |
20
|
page
|
int
|
page number |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 | |
prefetch_related(related)
Allows to prefetch related models during query - but opposite to
select_related each subsequent model is fetched in a separate database query.
With prefetch_related always one query per Model is run against the
database, meaning that you will have multiple queries executed one
after another.
To fetch related model use ForeignKey names.
To chain related Models relation use double underscores between names.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related
|
Union[list, str, Prefetch]
|
list of relation field names, can be linked by '__' to nest |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 | |
select_all(follow=False, max_depth=None, follow_reverse=0)
By default adds only directly related models.
If follow=True is set it adds also related models of related models.
To not get stuck in an infinite loop as related models also keep a relation to parent model visited models set is kept.
That way already visited models that are nested are loaded, but the load do not follow them inside. So Model A -> Model B -> Model C -> Model A -> Model X will load second Model A but will never follow into Model X. Nested relations of those kind need to be loaded manually.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
follow
|
bool
|
flag to trigger deep save - by default only directly related models are saved with follow=True also related models of related models are saved |
False
|
max_depth
|
Optional[int]
|
number of relations to follow at most with follow=True |
None
|
follow_reverse
|
Union[bool, int]
|
number of first levels where follow=True also
follows reverse and many to many relations, |
0
|
Returns:
| Type | Description |
|---|---|
Model
|
reloaded Model |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 | |
select_related(related)
Allows to prefetch related models during the same query.
With select_related always only one query is run against the database,
meaning that one (sometimes complicated) join is generated and later nested
models are processed in python.
To fetch related model use ForeignKey names.
To chain related Models relation use double underscores between names.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related
|
Union[list, str]
|
list of relation field names, can be linked by '__' to nest |
required |
Returns:
| Type | Description |
|---|---|
QuerysetProxy
|
QuerysetProxy |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 | |
sum(columns)
async
Returns sum value of columns for rows matching the given criteria
(applied with filter and exclude if set before).
Returns:
| Type | Description |
|---|---|
int
|
sum value of columns |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
263 264 265 266 267 268 269 270 271 | |
update(each=False, **kwargs)
async
Updates the model table after applying the filters from kwargs.
You have to either pass a filter to narrow down a query or explicitly pass each=True flag to affect whole table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
each
|
bool
|
flag if whole table should be affected if no filter is passed |
False
|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
number of updated rows |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | |
update_or_create(**kwargs)
async
Updates the model, or in case there is no match in database creates a new one.
If a pk is supplied but no row exists with that pk, a new row is
created with the supplied pk and field values rather than raising
NoMatch.
Actual call delegated to QuerySet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
Model
|
updated or created model |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | |
update_returning(each=False, **kwargs)
async
Updates the related models in one statement, like
QuerySet.update_returning(), and returns them as stored after the
update. Unlike update() it runs no per-model update(): no
signals are sent and fields of the through model are not updated.
You have to either pass a filter to narrow down a query or explicitly pass each=True flag to affect all related models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
each
|
bool
|
flag if all related models should be affected if no filter is passed |
False
|
kwargs
|
Any
|
fields names and proper value types, or column expressions |
{}
|
Returns:
| Type | Description |
|---|---|
list[Model]
|
updated models |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 | |
update_through_instance(child, **kwargs)
async
Updates a through model instance in the database for m2m relations. Without kwargs only checks that the through instance exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
dict of additional keyword arguments for through instance |
{}
|
child
|
T
|
child model instance |
required |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
upsert_through_instance(child, **kwargs)
async
Updates a through model instance in the database for m2m relations if it already exists, else creates one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
dict of additional keyword arguments for through instance |
{}
|
child
|
T
|
child model instance |
required |
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
values(fields=None, exclude_through=False)
async
Return a list of dictionaries with column values in order of the fields passed or all fields from queried models.
To filter for given row use filter/exclude methods before values, to limit number of rows use limit/offset or paginate before values.
Note that it always return a list even for one row from database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclude_through
|
bool
|
flag if through models should be excluded |
False
|
fields
|
Union[list, str, set, dict, None]
|
field name or list of field names to extract from db |
None
|
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
values_list(fields=None, flatten=False, exclude_through=False)
async
Return a list of tuples with column values in order of the fields passed or all fields from queried models.
When one field is passed you can flatten the list of tuples into list of values of that single field.
To filter for given row use filter/exclude methods before values, to limit number of rows use limit/offset or paginate before values.
Note that it always return a list even for one row from database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclude_through
|
bool
|
flag if through models should be excluded |
False
|
fields
|
Union[list, str, set, dict, None]
|
field name or list of field names to extract from db |
None
|
flatten
|
bool
|
when one field is passed you can flatten the list of tuples |
False
|
Source code in ormar/relations/querysetproxy.py
| Python | |
|---|---|
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 | |