queryset
QuerySet
Bases: Generic[T]
Main class to perform database queries, exposed on each model as objects attribute.
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 | |
model
property
Shortcut to model class set on QuerySet.
Returns:
| Type | Description |
|---|---|
type[Model]
|
model class |
model_config
property
Shortcut to model class OrmarConfig set on QuerySet model.
Returns:
| Type | Description |
|---|---|
model's OrmarConfig
|
OrmarConfig of the model |
table
property
Shortcut to models table from OrmarConfig.
Returns:
| Type | Description |
|---|---|
sqlalchemy.Table
|
database table |
__getitem__(key)
Returns a new QuerySet with LIMIT/OFFSET derived from a Python
integer index or slice.
Negative indices and negative slice bounds are translated into a
reversed-order query plus an in-memory list reversal on .all(),
so that Model.objects[-N:] returns the last N rows in the
original ordering. Slice shapes that would require a COUNT(*)
round-trip (a bare [:-N], or mixed positive/negative bounds) are
rejected with a QueryDefinitionError; use .count() combined
with .offset()/.limit() instead.
Each call replaces the previous pagination state rather than composing with it — subsequent slicing on an already-sliced queryset is not guaranteed to compose in a Python-list-like way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Union[int, slice]
|
integer index or slice |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
new QuerySet with updated pagination |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
when |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 | |
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/queryset/queryset.py
| Python | |
|---|---|
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 | |
build_select_expression(limit=None, offset=None, order_bys=None, binder=None)
Constructs the actual database query used in the QuerySet. If any of the params is not passed the QuerySet own value is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
Optional[int]
|
number to limit the query |
None
|
offset
|
Optional[int]
|
number to offset by |
None
|
order_bys
|
Optional[list]
|
list of order-by fields names |
None
|
binder
|
Optional[SlotBinder]
|
names per-call values for statement templates, if given |
None
|
Returns:
| Type | Description |
|---|---|
sqlalchemy.sql.selectable.Select
|
built sqlalchemy select expression |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
build_select_template(variant=(), limit=None, offset=None, order_bys=None, wrap=None)
Return the read statement for this queryset from the model's template cache (building and caching it on the first call of a cache key) together with the values to bind for this call::
| Text Only | |
|---|---|
1 2 3 | |
Every read that runs a select (all, get, first,
iterate, values, exists, count and the aggregates)
goes through here instead of build_select_expression. A statement
that is not a plain select passes wrap to build the executed
statement around the select and variant to keep its cache key apart
from the plain select's, e.g. count() passes
variant=("count", distinct) and wrap=count_statement.
On a cache hit nothing is built: only the cache key and the values are collected from the queryset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variant
|
tuple
|
how the select is used (plain, count, aggregate, ...) |
()
|
limit
|
Optional[int]
|
number to limit the query |
None
|
offset
|
Optional[int]
|
number to offset by |
None
|
order_bys
|
Optional[list]
|
list of order actions |
None
|
wrap
|
Optional[Callable[[Select], Any]]
|
turns the built select into the executed statement |
None
|
Returns:
| Type | Description |
|---|---|
StatementTemplate
|
statement and values to bind |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if a |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
build_template_statement(actions, limit, offset, order_bys, wrap)
Build the read statement of a new cache key with named value slots, the statement that is then cached.
The select is built by build_select_expression as usual, but with a
SlotBinder that turns every filter value, the limit and the offset
into a named bind parameter. wrap then builds the executed statement
around it, and the values of this first call are cleared from the
result (see release_values).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
list
|
filter actions in canonical order |
required |
limit
|
Optional[int]
|
number to limit the query |
required |
offset
|
Optional[int]
|
number to offset by |
required |
order_bys
|
Optional[list]
|
list of order actions |
required |
wrap
|
Optional[Callable[[Select], Any]]
|
turns the built select into the executed statement |
required |
Returns:
| Type | Description |
|---|---|
Any
|
statement to execute |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
build_write_template(statement, values)
Return the filtered update or delete of this queryset, or a statement
of update_returning() (see write_statements), from the model's
template cache, with the values to bind::
| Text Only | |
|---|---|
1 2 3 4 5 | |
The cache key is the statement class, the filter and exclude clauses
(see templates.clauses_cache_key), the joined relations (filters
on related models select the matched primary keys through them) and
the updated column names. Updates and deletes take no ordering or
limit, so nothing else changes their SQL. Filter values are named by
SlotBinder like in reads, updated values like in single-row writes
(ormar_v{i}).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
statement
|
type[WriteStatement]
|
statement class |
required |
values
|
dict
|
updated values by column name (empty for delete) |
required |
Returns:
| Type | Description |
|---|---|
StatementTemplate
|
statement and values to bind |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
bulk_create(objects, on_conflict=None, conflict_target=None, update_fields=None, update_values=None)
async
Performs a bulk create in one database session to speed up the process.
Allows you to create multiple objects at once.
A valid list of Model objects needs to be passed.
Objects that provide different sets of fields (e.g. an explicit pk or a server_default value) are inserted in separate statements, so generated primary keys may not follow the order of the passed list.
With on_conflict="ignore" rows conflicting with existing ones on any
unique key are skipped. With on_conflict="update" rows conflicting on
conflict_target (the primary key by default) update update_fields
(by default all inserted fields but the target and the key, none when
update_values are given), update_values and fields with
on_update. Skipped objects are marked as saved too.
Bulk operations do not send signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[T]
|
list of ormar models already initialized and ready to save. |
required |
on_conflict
|
Optional[Literal['ignore', 'update']]
|
|
None
|
conflict_target
|
Optional[list[str]]
|
names of the fields of the unique key the rows
conflict on, only with |
None
|
update_fields
|
Optional[list[str]]
|
names of the fields updated from the inserted
rows, only with |
None
|
update_values
|
Optional[dict[str, Any]]
|
values or expressions set on conflict by field
name, the same for every row, only with |
None
|
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 | |
bulk_update(objects, columns=None)
async
Performs bulk update in one database session to speed up the process.
Allows you to update multiple instance at once.
All Models passed need to have primary key column populated.
You can also select which fields to update by passing columns list
as a list of string names.
Bulk operations do not send signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[T]
|
list of ormar models |
required |
columns
|
Optional[list[str]]
|
list of columns to update |
None
|
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 | |
check_single_result_rows_count(rows)
staticmethod
Verifies if the result has one and only one row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
Sequence[Optional[T]]
|
one element list of Models |
required |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
367 368 369 370 371 372 373 374 375 376 377 378 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 | |
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.
With on_conflict="ignore" a row conflicting with an existing one on
any unique key is skipped and None is returned.
With on_conflict="update" a row conflicting on conflict_target
(the primary key by default) is updated instead, in the same statement:
update_fields (by default all passed fields but the target and the
key, none when update_values are given), update_values and the
fields with on_update. The stored row is returned. pre_save and
post_save are sent in both cases.
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 or stored model, |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
for |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 | |
delete(*args, each=False, **kwargs)
async
Deletes from 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.
Filters on related models (filter(training__instructor=instructor))
are supported, the delete removes the rows the same filters select in
all() (limit and offset do not apply).
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 deleted rows |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 | |
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)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
QuerySet
|
filtered QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Union[list, str, set, dict]
|
columns to exclude |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
exists()
async
Returns a bool value to confirm if there are rows matching the given criteria
(applied with filter and exclude if set).
Returns:
| Type | Description |
|---|---|
bool
|
result of the check |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 | |
fields(columns, slot='include')
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slot
|
Slot
|
which Excludable slot to write into ("include" or "exclude") |
'include'
|
columns
|
Union[list, str, set, dict]
|
columns to include |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
filter(*args, _exclude=False, **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)
Note that you can also use python style filters - check the docs!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_exclude
|
bool
|
flag if it should be exclude or filter |
False
|
kwargs
|
Any
|
fields names and proper value types |
{}
|
Returns:
| Type | Description |
|---|---|
QuerySet
|
filtered QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
first(*args, **kwargs)
async
Gets the first row from the db ordered by primary key column ascending.
Passing args and/or kwargs is a shortcut and equals to calling
filter(*args, **kwargs).first().
The query is always executed with LIMIT 1 so at most one row is
fetched; use :meth:get if you want to assert that the criteria
match exactly one row.
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/queryset/queryset.py
| Python | |
|---|---|
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 | |
flatten_fields(columns)
Render selected related models as their primary-key value on
model_dump() instead of the default nested dict.
Accepts the same input forms as :py:meth:fields / :py:meth:exclude_fields
plus FieldAccessor / list of accessors. Missing relations are
auto-loaded: single-valued foreign keys go into select_related,
many-to-many and reverse relations into prefetch_related.
Chained calls merge. A flatten directive conflicts with any include or exclude sub-field selection on the same relation (scalar output has no place for children).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Union[list, str, set, tuple, dict, FieldAccessor]
|
relations to flatten on serialization |
required |
Returns:
| Type | Description |
|---|---|
QuerySet[T]
|
new QuerySet with the flatten spec applied |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
get(*args, **kwargs)
async
Gets a single row from the db matching the criteria set by args/kwargs.
Passing args/kwargs is a shortcut for filter(*args, **kwargs).get().
When criteria are set (either through args/kwargs here or via a
chained :meth:filter/:meth:exclude) the query fetches every
matching row and asserts that exactly one comes back — NoMatch
is raised when none match and MultipleMatches when more than one
matches.
When no criteria are set, get() falls back to returning the last
row of the table ordered by primary key descending (LIMIT 1); in
that mode MultipleMatches cannot be raised.
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/queryset/queryset.py
| Python | |
|---|---|
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 | |
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.
Passing a criteria is actually calling filter(args, *kwargs) method described below.
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/queryset/queryset.py
| Python | |
|---|---|
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 | |
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/queryset/queryset.py
| Python | |
|---|---|
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 | |
last(*args, **kwargs)
async
Gets the last row from the db ordered by primary key column descending.
Complementary to :meth:first: the default pk ordering is flipped
and the top row is returned. Passing args and/or kwargs is a shortcut
and equals to calling filter(*args, **kwargs).last().
The query is always executed with LIMIT 1 so at most one row is
fetched; use :meth:get if you want to assert that the criteria
match exactly one row.
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/queryset/queryset.py
| Python | |
|---|---|
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 | |
limit(limit_count, limit_raw_sql=None)
You can limit the results to desired number of parent models.
To limit the actual number of database query rows instead of number of main
models use the limit_raw_sql parameter flag, and set it to True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit_raw_sql
|
Optional[bool]
|
flag if raw sql should be limited |
None
|
limit_count
|
int
|
number of models to limit |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 | |
limit_cache_key(limit)
Return how the effective limit changes the SQL: whether a LIMIT is
applied at all, and whether it is non-zero (only a non-zero limit with
select_related builds the pagination subquery). The limit value
itself is bound, so limit(5) and limit(10) share a statement::
| Text Only | |
|---|---|
1 2 3 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
Optional[int]
|
limit passed to |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
limit presence and truthiness |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 | |
matched_rows_clause(binder)
Return the condition keeping the rows this queryset selects, by primary key, for an update or delete filtering on related models::
| Text Only | |
|---|---|
1 2 3 4 | |
The select is wrapped in a derived table, as MySQL does not let a statement select from the table it updates or deletes from (error 1093). A composite primary key is compared as a tuple.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binder
|
Optional[SlotBinder]
|
names filter values for templates, None binds them as plain values |
required |
Returns:
| Type | Description |
|---|---|
Any
|
condition on the primary key columns |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
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/queryset/queryset.py
| Python | |
|---|---|
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 | |
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/queryset/queryset.py
| Python | |
|---|---|
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 | |
offset(offset, limit_raw_sql=None)
You can also offset the results by desired number of main models.
To offset the actual number of database query rows instead of number of main
models use the limit_raw_sql parameter flag, and set it to True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit_raw_sql
|
Optional[bool]
|
flag if raw sql should be offset |
None
|
offset
|
int
|
numbers of models to offset |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 | |
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()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Union[list, str, OrderAction, RandomOrder, Random]
|
columns by which models should be sorted |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related
|
Union[list, str, FieldAccessor, Prefetch]
|
list of relation field names, can be linked by '__' to nest |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
rebuild_self(filter_clauses=None, exclude_clauses=None, select_related=None, limit_count=None, offset=None, excludable=None, order_bys=None, prefetch_related=None, limit_raw_sql=None, proxy_source_model=None, reverse_result=None, prefetch_querysets=None, row_lock=None)
Method that returns new instance of queryset based on passed params, all not passed params are taken from current values.
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
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, forward foreign keys |
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/queryset/queryset.py
| Python | |
|---|---|
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 | |
select_cache_key(variant, limit, offset, order_bys)
Return the cache key of the statement build_select_expression would build
with the given arguments: everything that changes its SQL text and none
of the bound values. Two querysets with the same key must build the
same SQL, or one would run the other's statement, so each part covers
one input of the build:
variant- plain select, exists, count or aggregate- filter and exclude clauses - see
clauses_cache_key select_related- the joins- selected fields - see
excludable_cache_key - ordering - see
order_cache_key - limit and offset presence - see
limit_cache_key limit_raw_sql- limit on raw rows instead of main modelsrow_lock- theselect_for_updateoptions
Any new queryset option that changes the built SQL has to be added here; the cross check of the test suite fails when one is missing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variant
|
tuple
|
how the select is used (plain, count, aggregate, ...) |
required |
limit
|
Optional[int]
|
limit passed to |
required |
offset
|
Optional[int]
|
offset passed to |
required |
order_bys
|
Optional[list]
|
order actions passed to |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
hashable cache key of the statement |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 | |
select_for_update(nowait=False, skip_locked=False, read=False, no_key=False)
Lock the rows of the main model read by this queryset until the end of
the transaction (SELECT ... FOR UPDATE OF <table>). It has to run
inside a transaction; joined related models are not locked and SQLite
runs the query without locks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nowait
|
bool
|
raise instead of waiting for rows locked by others |
False
|
skip_locked
|
bool
|
skip rows locked by others instead of waiting |
False
|
read
|
bool
|
take a shared lock ( |
False
|
no_key
|
bool
|
weaker PostgreSQL lock that does not block inserts of
rows referencing the locked ones ( |
False
|
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if both nowait and skip_locked are set |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 | |
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
related
|
Union[list, str, FieldAccessor]
|
list of relation field names, can be linked by '__' to nest |
required |
Returns:
| Type | Description |
|---|---|
QuerySet
|
QuerySet |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 | |
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/queryset/queryset.py
| Python | |
|---|---|
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 | |
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.
Filters on related models (filter(album__name="A")) are supported,
the update touches the rows the same filters select in all() (limit
and offset do not apply). Expressions may read fields of models reached
through foreign keys (position=F("album__rank")); rows without a
related row on such a path are not updated and not counted.
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, or column expressions
( |
{}
|
Returns:
| Type | Description |
|---|---|
int
|
number of updated rows |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if an expression sets a relation, reads a reverse or many to many relation or reads another column set by this update |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 | |
update_locked_rows(updates)
async
Update the rows of update_returning() without RETURNING, in the
open transaction or a new one, and return them as stored::
| Text Only | |
|---|---|
1 2 3 | |
The lock keeps other transactions from changing the rows in between, and the locking read back sees the latest committed versions (not the snapshot of a REPEATABLE READ transaction), so the rows read back are the rows as this update left them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
updates
|
dict
|
updated values by column name |
required |
Returns:
| Type | Description |
|---|---|
list
|
rows of the updated models |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 | |
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.
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/queryset/queryset.py
| Python | |
|---|---|
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 | |
update_returning(each=False, **kwargs)
async
Updates the rows like update() and returns them as stored after the
update::
| Text Only | |
|---|---|
1 2 3 | |
The models hold the columns of the model's own table only: relations
are not loaded, foreign keys are models with only the primary key set,
like all() without select_related. fields(),
select_related() and the ordering of the queryset do not apply,
the rows come back in no particular order. No signals are sent.
PostgreSQL and SQLite (3.35+) run one UPDATE ... RETURNING. MySQL
has no RETURNING: in the open transaction, or a new one, the rows
are selected FOR UPDATE, updated and read back by primary key.
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, or column expressions |
{}
|
Returns:
| Type | Description |
|---|---|
list[Model]
|
updated models |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
like |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 | |
update_values(each, values)
Check an update() and turn its keyword arguments into the values
to write, by column name: own fields only, on_update values added,
plain values validated and converted, expressions as expression nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
each
|
bool
|
flag if whole table should be affected if no filter is passed |
required |
values
|
dict
|
keyword arguments of the update |
required |
Returns:
| Type | Description |
|---|---|
dict
|
values to write by column name |
Raises:
| Type | Description |
|---|---|
QueryDefinitionError
|
if the queryset has no filters and
|
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 | |
values(fields=None, exclude_through=False, _as_dict=True, _flatten=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
|
_flatten
|
bool
|
internal parameter to flatten one element tuples |
False
|
_as_dict
|
bool
|
internal parameter if return dict or tuples |
True
|
fields
|
Union[list, str, set, dict, None]
|
field name or list of field names to extract from db |
None
|
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
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 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 | |
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/queryset/queryset.py
| Python | |
|---|---|
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 | |
dump_for_save(obj)
Return the field values of a model with flat_row_model() for
bulk_create.
Without relations, converted fields or custom serializers, pydantic's
model_dump() returns the same values as a copy of the __dict__,
at a fraction of the cost::
| Text Only | |
|---|---|
1 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Model
|
model to save |
required |
Returns:
| Type | Description |
|---|---|
dict
|
field values |
Source code in ormar/queryset/queryset.py
| Python | |
|---|---|
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |