rakko-orm 1.0.0a0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rakko/__init__.py +613 -0
- rakko/__main__.py +10 -0
- rakko/apps.py +356 -0
- rakko/backends/__init__.py +4 -0
- rakko/backends/asyncpg/__init__.py +7 -0
- rakko/backends/asyncpg/client.py +223 -0
- rakko/backends/asyncpg/executor.py +20 -0
- rakko/backends/asyncpg/schema_generator.py +17 -0
- rakko/backends/base/__init__.py +4 -0
- rakko/backends/base/client.py +438 -0
- rakko/backends/base/config_generator.py +249 -0
- rakko/backends/base/executor.py +764 -0
- rakko/backends/base/schema_generator.py +580 -0
- rakko/backends/base_postgres/__init__.py +4 -0
- rakko/backends/base_postgres/client.py +170 -0
- rakko/backends/base_postgres/executor.py +149 -0
- rakko/backends/base_postgres/schema_generator.py +84 -0
- rakko/backends/mssql/__init__.py +7 -0
- rakko/backends/mssql/client.py +127 -0
- rakko/backends/mssql/executor.py +17 -0
- rakko/backends/mssql/schema_generator.py +125 -0
- rakko/backends/mysql/__init__.py +7 -0
- rakko/backends/mysql/client.py +312 -0
- rakko/backends/mysql/executor.py +154 -0
- rakko/backends/mysql/schema_generator.py +101 -0
- rakko/backends/odbc/__init__.py +4 -0
- rakko/backends/odbc/client.py +221 -0
- rakko/backends/odbc/executor.py +20 -0
- rakko/backends/oracle/__init__.py +7 -0
- rakko/backends/oracle/client.py +136 -0
- rakko/backends/oracle/executor.py +27 -0
- rakko/backends/oracle/schema_generator.py +120 -0
- rakko/backends/psycopg/__init__.py +7 -0
- rakko/backends/psycopg/client.py +288 -0
- rakko/backends/psycopg/executor.py +14 -0
- rakko/backends/psycopg/schema_generator.py +17 -0
- rakko/backends/sqlite/__init__.py +14 -0
- rakko/backends/sqlite/client.py +299 -0
- rakko/backends/sqlite/executor.py +88 -0
- rakko/backends/sqlite/schema_generator.py +32 -0
- rakko/cli/__init__.py +13 -0
- rakko/cli/cli.py +878 -0
- rakko/cli/utils.py +181 -0
- rakko/config.py +315 -0
- rakko/connection.py +345 -0
- rakko/context.py +571 -0
- rakko/contrib/__init__.py +4 -0
- rakko/contrib/aiohttp/__init__.py +99 -0
- rakko/contrib/blacksheep/__init__.py +121 -0
- rakko/contrib/fastapi/__init__.py +301 -0
- rakko/contrib/mysql/__init__.py +4 -0
- rakko/contrib/mysql/fields.py +63 -0
- rakko/contrib/mysql/functions.py +19 -0
- rakko/contrib/mysql/indexes.py +28 -0
- rakko/contrib/mysql/json_functions.py +80 -0
- rakko/contrib/mysql/search.py +49 -0
- rakko/contrib/postgres/__init__.py +4 -0
- rakko/contrib/postgres/array_functions.py +35 -0
- rakko/contrib/postgres/fields.py +105 -0
- rakko/contrib/postgres/functions.py +43 -0
- rakko/contrib/postgres/indexes.py +33 -0
- rakko/contrib/postgres/json_functions.py +113 -0
- rakko/contrib/postgres/regex.py +31 -0
- rakko/contrib/postgres/search.py +483 -0
- rakko/contrib/pydantic/__init__.py +16 -0
- rakko/contrib/pydantic/base.py +147 -0
- rakko/contrib/pydantic/creator.py +681 -0
- rakko/contrib/pydantic/descriptions.py +207 -0
- rakko/contrib/pydantic/utils.py +38 -0
- rakko/contrib/pylint/__init__.py +158 -0
- rakko/contrib/quart/__init__.py +117 -0
- rakko/contrib/sanic/__init__.py +116 -0
- rakko/contrib/sqlite/__init__.py +4 -0
- rakko/contrib/sqlite/functions.py +16 -0
- rakko/contrib/sqlite/regex.py +46 -0
- rakko/contrib/starlette/__init__.py +180 -0
- rakko/contrib/test/__init__.py +336 -0
- rakko/contrib/test/condition.py +42 -0
- rakko/converters.py +160 -0
- rakko/exceptions.py +130 -0
- rakko/expressions.py +962 -0
- rakko/fields/__init__.py +111 -0
- rakko/fields/base.py +685 -0
- rakko/fields/data.py +1308 -0
- rakko/fields/db_defaults.py +105 -0
- rakko/fields/relational.py +703 -0
- rakko/filters.py +639 -0
- rakko/functions.py +142 -0
- rakko/indexes.py +165 -0
- rakko/log.py +8 -0
- rakko/manager.py +29 -0
- rakko/migrations/__init__.py +41 -0
- rakko/migrations/api/__init__.py +16 -0
- rakko/migrations/api/migrate.py +77 -0
- rakko/migrations/api/plan.py +76 -0
- rakko/migrations/api/sqlmigrate.py +69 -0
- rakko/migrations/autodetector.py +203 -0
- rakko/migrations/constraints.py +31 -0
- rakko/migrations/exceptions.py +10 -0
- rakko/migrations/executor.py +368 -0
- rakko/migrations/expressions.py +13 -0
- rakko/migrations/graph.py +158 -0
- rakko/migrations/loader.py +127 -0
- rakko/migrations/migration.py +189 -0
- rakko/migrations/operations.py +1288 -0
- rakko/migrations/recorder.py +104 -0
- rakko/migrations/schema_editor/__init__.py +25 -0
- rakko/migrations/schema_editor/asyncpg.py +11 -0
- rakko/migrations/schema_editor/base.py +945 -0
- rakko/migrations/schema_editor/base_postgres.py +171 -0
- rakko/migrations/schema_editor/data.py +18 -0
- rakko/migrations/schema_editor/mssql.py +339 -0
- rakko/migrations/schema_editor/mysql.py +408 -0
- rakko/migrations/schema_editor/oracle.py +259 -0
- rakko/migrations/schema_editor/psycopg.py +11 -0
- rakko/migrations/schema_editor/sqlite.py +424 -0
- rakko/migrations/schema_generator/__init__.py +5 -0
- rakko/migrations/schema_generator/operation_generator.py +163 -0
- rakko/migrations/schema_generator/state.py +224 -0
- rakko/migrations/schema_generator/state_apps.py +165 -0
- rakko/migrations/schema_generator/state_diff.py +419 -0
- rakko/migrations/writer.py +515 -0
- rakko/models.py +1854 -0
- rakko/py.typed +0 -0
- rakko/query_api.py +134 -0
- rakko/query_utils.py +238 -0
- rakko/queryset.py +2743 -0
- rakko/relations.py +85 -0
- rakko/router.py +55 -0
- rakko/schema_quoting.py +62 -0
- rakko/signals.py +73 -0
- rakko/timezone.py +173 -0
- rakko/transactions.py +69 -0
- rakko/utils.py +50 -0
- rakko/validators.py +412 -0
- rakko/warnings.py +22 -0
- rakko_orm-1.0.0a0.dist-info/METADATA +114 -0
- rakko_orm-1.0.0a0.dist-info/RECORD +143 -0
- rakko_orm-1.0.0a0.dist-info/WHEEL +4 -0
- rakko_orm-1.0.0a0.dist-info/entry_points.txt +5 -0
- rakko_orm-1.0.0a0.dist-info/licenses/CONTRIBUTORS.rst +86 -0
- rakko_orm-1.0.0a0.dist-info/licenses/LICENSE.txt +201 -0
- rakko_orm-1.0.0a0.dist-info/licenses/NOTICE.txt +22 -0
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
# Copyright (c) 2026 ropmyung (Rakko ORM modifications and additions).
|
|
2
|
+
# Derived from Tortoise ORM; maintained and modified for Rakko ORM.
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import datetime
|
|
9
|
+
import decimal
|
|
10
|
+
from collections.abc import Callable, Iterable, Sequence
|
|
11
|
+
from copy import copy
|
|
12
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
13
|
+
|
|
14
|
+
from pypika_tortoise.queries import QueryBuilder, Table
|
|
15
|
+
from pypika_tortoise.terms import Parameter
|
|
16
|
+
|
|
17
|
+
from rakko.exceptions import OperationalError, UnSupportedError
|
|
18
|
+
from rakko.expressions import Expression, ResolveContext
|
|
19
|
+
from rakko.fields.base import DatabaseDefault
|
|
20
|
+
from rakko.fields.relational import RelationalField
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING: # pragma: nocoverage
|
|
23
|
+
from rakko.backends.base.client import BaseDBAsyncClient
|
|
24
|
+
from rakko.fields.relational import (
|
|
25
|
+
BackwardFKRelation,
|
|
26
|
+
BackwardOneToOneRelation,
|
|
27
|
+
ManyToManyFieldInstance,
|
|
28
|
+
)
|
|
29
|
+
from rakko.filters import FilterInfoDict
|
|
30
|
+
from rakko.models import MODEL, Model
|
|
31
|
+
from rakko.query_utils import Prefetch
|
|
32
|
+
from rakko.queryset import QuerySet
|
|
33
|
+
|
|
34
|
+
EXECUTOR_CACHE: dict[
|
|
35
|
+
tuple[str, str | None, str],
|
|
36
|
+
tuple[list, str, list, str, str, dict[str, str]],
|
|
37
|
+
] = {}
|
|
38
|
+
|
|
39
|
+
CHUNK_SIZE = 2000
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class BaseExecutor:
|
|
43
|
+
FILTER_FUNC_OVERRIDE: dict[Callable, Callable] = {}
|
|
44
|
+
EXPLAIN_PREFIX: str = "EXPLAIN"
|
|
45
|
+
DB_NATIVE = {bytes, str, int, float, decimal.Decimal, datetime.datetime, datetime.date}
|
|
46
|
+
|
|
47
|
+
def __init__(
|
|
48
|
+
self,
|
|
49
|
+
model: type[Model],
|
|
50
|
+
db: BaseDBAsyncClient,
|
|
51
|
+
prefetch_map: dict[str, set[str | Prefetch]] | None = None,
|
|
52
|
+
prefetch_queries: dict[str, list[tuple[str | None, QuerySet]]] | None = None,
|
|
53
|
+
select_related_idx: (
|
|
54
|
+
list[tuple[type[Model], int, str, type[Model], Iterable[str | None]]] | None
|
|
55
|
+
) = None,
|
|
56
|
+
) -> None:
|
|
57
|
+
self.model = model
|
|
58
|
+
self.db: BaseDBAsyncClient = db
|
|
59
|
+
self.prefetch_map = prefetch_map or {}
|
|
60
|
+
self._prefetch_queries = prefetch_queries or {}
|
|
61
|
+
self.select_related_idx = select_related_idx
|
|
62
|
+
key = (self.db.connection_name, self.model._meta.schema, self.model._meta.db_table)
|
|
63
|
+
if key not in EXECUTOR_CACHE:
|
|
64
|
+
self.regular_columns, columns = self._prepare_insert_columns()
|
|
65
|
+
self.insert_query = str(self._prepare_insert_statement(columns))
|
|
66
|
+
self.regular_columns_all = self.regular_columns
|
|
67
|
+
self.insert_query_all = self.insert_query
|
|
68
|
+
if self.model._meta.generated_db_fields:
|
|
69
|
+
self.regular_columns_all, columns_all = self._prepare_insert_columns(
|
|
70
|
+
include_generated=True
|
|
71
|
+
)
|
|
72
|
+
self.insert_query_all = str(
|
|
73
|
+
self._prepare_insert_statement(columns_all, has_generated=False)
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
table = self.model._meta.basetable
|
|
77
|
+
basequery = cast(QueryBuilder, self.model._meta.basequery)
|
|
78
|
+
self.delete_query = str(
|
|
79
|
+
basequery.where(table[self.model._meta.db_pk_column] == self.parameter(0)).delete()
|
|
80
|
+
)
|
|
81
|
+
self.update_cache: dict[str, str] = {}
|
|
82
|
+
|
|
83
|
+
EXECUTOR_CACHE[key] = (
|
|
84
|
+
self.regular_columns,
|
|
85
|
+
self.insert_query,
|
|
86
|
+
self.regular_columns_all,
|
|
87
|
+
self.insert_query_all,
|
|
88
|
+
self.delete_query,
|
|
89
|
+
self.update_cache,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
else:
|
|
93
|
+
(
|
|
94
|
+
self.regular_columns,
|
|
95
|
+
self.insert_query,
|
|
96
|
+
self.regular_columns_all,
|
|
97
|
+
self.insert_query_all,
|
|
98
|
+
self.delete_query,
|
|
99
|
+
self.update_cache,
|
|
100
|
+
) = EXECUTOR_CACHE[key]
|
|
101
|
+
|
|
102
|
+
async def execute_explain(
|
|
103
|
+
self, sql: str, output_fmt: str | None = None, **options: bool
|
|
104
|
+
) -> Any:
|
|
105
|
+
if output_fmt:
|
|
106
|
+
raise UnSupportedError("This database does not support different explain formats")
|
|
107
|
+
|
|
108
|
+
if options:
|
|
109
|
+
raise UnSupportedError("This database does not support explain options")
|
|
110
|
+
|
|
111
|
+
sql = " ".join((self.EXPLAIN_PREFIX, sql))
|
|
112
|
+
return (await self.db.execute_query(sql))[1]
|
|
113
|
+
|
|
114
|
+
async def execute_select(
|
|
115
|
+
self,
|
|
116
|
+
sql: str,
|
|
117
|
+
values: list | None = None,
|
|
118
|
+
custom_fields: list | None = None,
|
|
119
|
+
) -> list:
|
|
120
|
+
_, raw_results = await self.db.execute_query(sql, values)
|
|
121
|
+
instance_list = []
|
|
122
|
+
_split_cache: dict[str, str] = {}
|
|
123
|
+
for row_idx, row in enumerate(raw_results):
|
|
124
|
+
if row_idx != 0 and row_idx % CHUNK_SIZE == 0:
|
|
125
|
+
# Forcibly yield to the event loop to avoid blocking the event loop
|
|
126
|
+
# when selecting a large number of rows
|
|
127
|
+
await asyncio.sleep(0)
|
|
128
|
+
|
|
129
|
+
if self.select_related_idx:
|
|
130
|
+
_, current_idx, _, _, path = self.select_related_idx[0]
|
|
131
|
+
row_items = list(dict(row).items())
|
|
132
|
+
instance: Model = self.model._init_from_db(**dict(row_items[:current_idx]))
|
|
133
|
+
instances: dict[Any, Any] = {path: instance}
|
|
134
|
+
for model, index, *__, full_path in self.select_related_idx[1:]:
|
|
135
|
+
(*path, attr) = full_path
|
|
136
|
+
related_items = row_items[current_idx : current_idx + index]
|
|
137
|
+
if any(v for _, v in related_items):
|
|
138
|
+
related_kwargs = {}
|
|
139
|
+
for k, v in related_items:
|
|
140
|
+
fname = _split_cache.get(k)
|
|
141
|
+
if fname is None:
|
|
142
|
+
fname = _split_cache[k] = k.split(".", 1)[1]
|
|
143
|
+
related_kwargs[fname] = v
|
|
144
|
+
obj = model._init_from_db(**related_kwargs)
|
|
145
|
+
elif index == 0:
|
|
146
|
+
# 0 signals that an empty "filler" object should be created in the case
|
|
147
|
+
# where a field of related model is selected but model itself isn't,
|
|
148
|
+
# e.g. .only("relatedmodel__field")
|
|
149
|
+
obj = model._init_from_db()
|
|
150
|
+
else:
|
|
151
|
+
obj = None
|
|
152
|
+
target = instances.get(tuple(path))
|
|
153
|
+
if target is not None:
|
|
154
|
+
object.__setattr__(target, f"_{attr}", obj)
|
|
155
|
+
if obj is not None:
|
|
156
|
+
instances[(*path, attr)] = obj
|
|
157
|
+
current_idx += index
|
|
158
|
+
else:
|
|
159
|
+
instance = self.model._init_from_db(**row)
|
|
160
|
+
if custom_fields:
|
|
161
|
+
for field in custom_fields:
|
|
162
|
+
object.__setattr__(instance, field, row[field])
|
|
163
|
+
instance_list.append(instance)
|
|
164
|
+
await self._execute_prefetch_queries(instance_list)
|
|
165
|
+
return instance_list
|
|
166
|
+
|
|
167
|
+
async def execute_union(
|
|
168
|
+
self, sql: str, app_field: str, model_field: str, models: set[type[Model]]
|
|
169
|
+
) -> list:
|
|
170
|
+
_, raw_results = await self.db.execute_query(sql)
|
|
171
|
+
instance_list = []
|
|
172
|
+
|
|
173
|
+
for row_idx, row in enumerate(raw_results):
|
|
174
|
+
if row_idx != 0 and row_idx % CHUNK_SIZE == 0:
|
|
175
|
+
# Forcibly yield to the event loop to avoid blocking the event loop
|
|
176
|
+
# when selecting a large number of rows
|
|
177
|
+
await asyncio.sleep(0)
|
|
178
|
+
|
|
179
|
+
for model in models:
|
|
180
|
+
if (
|
|
181
|
+
model._meta.app == row[app_field]
|
|
182
|
+
and model._meta._model.__name__ == row[model_field]
|
|
183
|
+
):
|
|
184
|
+
row = {
|
|
185
|
+
field: row[field]
|
|
186
|
+
for field in row.keys()
|
|
187
|
+
if field not in (model_field, app_field)
|
|
188
|
+
}
|
|
189
|
+
instance_list.append(model._init_from_db(**row))
|
|
190
|
+
break
|
|
191
|
+
|
|
192
|
+
return instance_list
|
|
193
|
+
|
|
194
|
+
def _prepare_insert_columns(
|
|
195
|
+
self, include_generated: bool = False
|
|
196
|
+
) -> tuple[list[str], list[str]]:
|
|
197
|
+
regular_columns = []
|
|
198
|
+
for column in self.model._meta.fields_db_projection:
|
|
199
|
+
field_object = self.model._meta.fields_map[column]
|
|
200
|
+
if include_generated or not field_object.generated:
|
|
201
|
+
regular_columns.append(column)
|
|
202
|
+
result_columns = [self.model._meta.fields_db_projection[c] for c in regular_columns]
|
|
203
|
+
return regular_columns, result_columns
|
|
204
|
+
|
|
205
|
+
def _prepare_insert_statement(
|
|
206
|
+
self, columns: Sequence[str], has_generated: bool = True, ignore_conflicts: bool = False
|
|
207
|
+
) -> QueryBuilder:
|
|
208
|
+
# Insert should implement returning new id to saved object
|
|
209
|
+
# Each db has its own methods for it, so each implementation should
|
|
210
|
+
# go to descendant executors
|
|
211
|
+
query = (
|
|
212
|
+
self.db.query_class.into(self.model._meta.basetable)
|
|
213
|
+
.columns(*columns)
|
|
214
|
+
.insert(*[self.parameter(i) for i in range(len(columns))])
|
|
215
|
+
)
|
|
216
|
+
if ignore_conflicts:
|
|
217
|
+
query = query.on_conflict().do_nothing()
|
|
218
|
+
return query
|
|
219
|
+
|
|
220
|
+
async def _process_insert_result(self, instance: Model, results: Any) -> None:
|
|
221
|
+
raise NotImplementedError() # pragma: nocoverage
|
|
222
|
+
|
|
223
|
+
def parameter(self, pos: int) -> Parameter:
|
|
224
|
+
return Parameter(idx=pos + 1)
|
|
225
|
+
|
|
226
|
+
def _has_db_default_values(self, instance: Model, columns: list[str]) -> bool:
|
|
227
|
+
"""Check whether any column on the instance still holds a DatabaseDefault sentinel."""
|
|
228
|
+
|
|
229
|
+
if not self.model._meta.db_default_db_columns:
|
|
230
|
+
return False
|
|
231
|
+
for field_name in columns:
|
|
232
|
+
if isinstance(getattr(instance, field_name, None), DatabaseDefault):
|
|
233
|
+
return True
|
|
234
|
+
return False
|
|
235
|
+
|
|
236
|
+
def _build_insert_with_defaults(
|
|
237
|
+
self,
|
|
238
|
+
instance: Model,
|
|
239
|
+
columns: list[str] | None = None,
|
|
240
|
+
has_generated: bool = True,
|
|
241
|
+
ignore_conflicts: bool = False,
|
|
242
|
+
) -> tuple[str, list[Any], list[str]]:
|
|
243
|
+
"""Build an INSERT query where DatabaseDefault fields are omitted.
|
|
244
|
+
|
|
245
|
+
Returns ``(sql_string, values_list, db_default_columns)`` where:
|
|
246
|
+
- *values_list* only contains bind values for non-DatabaseDefault fields.
|
|
247
|
+
- *db_default_columns* lists the DB column names that were omitted from
|
|
248
|
+
the INSERT (they will use the DB-level DEFAULT).
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
if columns is None:
|
|
252
|
+
columns = self.regular_columns
|
|
253
|
+
|
|
254
|
+
active_db_columns: list[str] = []
|
|
255
|
+
values: list[Any] = []
|
|
256
|
+
db_default_columns: list[str] = []
|
|
257
|
+
|
|
258
|
+
for field_name in columns:
|
|
259
|
+
field_object = self.model._meta.fields_map[field_name]
|
|
260
|
+
value = getattr(instance, field_name)
|
|
261
|
+
db_col = self.model._meta.fields_db_projection[field_name]
|
|
262
|
+
if isinstance(value, DatabaseDefault):
|
|
263
|
+
db_default_columns.append(db_col)
|
|
264
|
+
else:
|
|
265
|
+
active_db_columns.append(db_col)
|
|
266
|
+
values.append(field_object.to_db_value(value, instance))
|
|
267
|
+
|
|
268
|
+
table = self.model._meta.basetable
|
|
269
|
+
|
|
270
|
+
if active_db_columns:
|
|
271
|
+
insert_terms = [self.parameter(i) for i in range(len(active_db_columns))]
|
|
272
|
+
query = (
|
|
273
|
+
self.db.query_class.into(table).columns(*active_db_columns).insert(*insert_terms)
|
|
274
|
+
)
|
|
275
|
+
else:
|
|
276
|
+
query = self.db.query_class.into(table).default_values()
|
|
277
|
+
|
|
278
|
+
query = self._add_returning_to_insert(query, has_generated, db_default_columns)
|
|
279
|
+
if ignore_conflicts:
|
|
280
|
+
query = query.on_conflict().do_nothing()
|
|
281
|
+
return str(query), values, db_default_columns
|
|
282
|
+
|
|
283
|
+
def _get_returning_fields(
|
|
284
|
+
self,
|
|
285
|
+
has_generated: bool,
|
|
286
|
+
db_default_columns: list[str],
|
|
287
|
+
) -> list[str]:
|
|
288
|
+
"""Compute the list of column names for a RETURNING clause.
|
|
289
|
+
|
|
290
|
+
Combines generated fields (when *has_generated* is True) with
|
|
291
|
+
*db_default_columns*, deduplicating in order.
|
|
292
|
+
"""
|
|
293
|
+
returning_fields: list[str] = []
|
|
294
|
+
if has_generated and (generated_fields := self.model._meta.generated_db_fields):
|
|
295
|
+
returning_fields.extend(generated_fields)
|
|
296
|
+
for col in db_default_columns:
|
|
297
|
+
if col not in returning_fields:
|
|
298
|
+
returning_fields.append(col)
|
|
299
|
+
return returning_fields
|
|
300
|
+
|
|
301
|
+
def _add_returning_to_insert(
|
|
302
|
+
self,
|
|
303
|
+
query: QueryBuilder,
|
|
304
|
+
has_generated: bool,
|
|
305
|
+
db_default_columns: list[str],
|
|
306
|
+
) -> QueryBuilder:
|
|
307
|
+
"""Hook for backends to add RETURNING clause.
|
|
308
|
+
|
|
309
|
+
Base implementation does nothing (backends without RETURNING support).
|
|
310
|
+
Override in PostgreSQL and SQLite executors.
|
|
311
|
+
"""
|
|
312
|
+
return query
|
|
313
|
+
|
|
314
|
+
async def _execute_insert_dynamic(
|
|
315
|
+
self,
|
|
316
|
+
instance: Model,
|
|
317
|
+
columns: list[str],
|
|
318
|
+
has_generated: bool,
|
|
319
|
+
) -> Any:
|
|
320
|
+
"""Execute a dynamic INSERT (with DEFAULT keywords) and return the raw result.
|
|
321
|
+
|
|
322
|
+
Calls ``_build_insert_with_defaults`` to produce the SQL, then
|
|
323
|
+
executes it via the DB client.
|
|
324
|
+
"""
|
|
325
|
+
query, values, _db_default_columns = self._build_insert_with_defaults(
|
|
326
|
+
instance, columns=columns, has_generated=has_generated
|
|
327
|
+
)
|
|
328
|
+
return await self.db.execute_insert(query, values)
|
|
329
|
+
|
|
330
|
+
async def execute_insert(self, instance: Model) -> None:
|
|
331
|
+
if not instance._custom_generated_pk:
|
|
332
|
+
has_db_defaults = self._has_db_default_values(instance, self.regular_columns)
|
|
333
|
+
|
|
334
|
+
if has_db_defaults:
|
|
335
|
+
insert_result = await self._execute_insert_dynamic(
|
|
336
|
+
instance, self.regular_columns, has_generated=True
|
|
337
|
+
)
|
|
338
|
+
else:
|
|
339
|
+
values = [
|
|
340
|
+
self.model._meta.fields_map[field_name].to_db_value(
|
|
341
|
+
getattr(instance, field_name), instance
|
|
342
|
+
)
|
|
343
|
+
for field_name in self.regular_columns
|
|
344
|
+
]
|
|
345
|
+
insert_result = await self.db.execute_insert(self.insert_query, values)
|
|
346
|
+
await self._process_insert_result(instance, insert_result)
|
|
347
|
+
|
|
348
|
+
else:
|
|
349
|
+
has_db_defaults = self._has_db_default_values(instance, self.regular_columns_all)
|
|
350
|
+
|
|
351
|
+
if has_db_defaults:
|
|
352
|
+
insert_result = await self._execute_insert_dynamic(
|
|
353
|
+
instance, self.regular_columns_all, has_generated=False
|
|
354
|
+
)
|
|
355
|
+
await self._process_insert_result(instance, insert_result)
|
|
356
|
+
else:
|
|
357
|
+
values = [
|
|
358
|
+
self.model._meta.fields_map[field_name].to_db_value(
|
|
359
|
+
getattr(instance, field_name), instance
|
|
360
|
+
)
|
|
361
|
+
for field_name in self.regular_columns_all
|
|
362
|
+
]
|
|
363
|
+
await self.db.execute_insert(self.insert_query_all, values)
|
|
364
|
+
|
|
365
|
+
async def _fetch_db_defaults_after_insert(self, instance: Model) -> None:
|
|
366
|
+
"""Fetch DB-applied default values via SELECT after INSERT.
|
|
367
|
+
|
|
368
|
+
Called only for non-RETURNING backends when db_default fields
|
|
369
|
+
were set to DEFAULT in the INSERT.
|
|
370
|
+
Guarded by Meta.fetch_db_defaults.
|
|
371
|
+
"""
|
|
372
|
+
|
|
373
|
+
if not self.model._meta.fetch_db_defaults:
|
|
374
|
+
return
|
|
375
|
+
|
|
376
|
+
db_default_db_columns = self.model._meta.db_default_db_columns
|
|
377
|
+
if not db_default_db_columns:
|
|
378
|
+
return
|
|
379
|
+
|
|
380
|
+
# Determine which fields still have DatabaseDefault (not populated by RETURNING)
|
|
381
|
+
fields_to_fetch: list[str] = []
|
|
382
|
+
db_projection_reverse = self.model._meta.fields_db_projection_reverse
|
|
383
|
+
for db_col in db_default_db_columns:
|
|
384
|
+
model_field = db_projection_reverse.get(db_col, db_col)
|
|
385
|
+
if isinstance(getattr(instance, model_field, None), DatabaseDefault):
|
|
386
|
+
fields_to_fetch.append(db_col)
|
|
387
|
+
|
|
388
|
+
if not fields_to_fetch:
|
|
389
|
+
return
|
|
390
|
+
|
|
391
|
+
# Need PK to SELECT
|
|
392
|
+
if instance.pk is None:
|
|
393
|
+
return
|
|
394
|
+
|
|
395
|
+
# Build SELECT via pypika for proper quoting
|
|
396
|
+
table = self.model._meta.basetable
|
|
397
|
+
pk_col = self.model._meta.db_pk_column
|
|
398
|
+
query = (
|
|
399
|
+
self.db.query_class.from_(table)
|
|
400
|
+
.select(*fields_to_fetch)
|
|
401
|
+
.where(table[pk_col] == self.parameter(0))
|
|
402
|
+
)
|
|
403
|
+
pk_value = self.model._meta.pk.to_db_value(instance.pk, instance)
|
|
404
|
+
_, rows = await self.db.execute_query(str(query), [pk_value])
|
|
405
|
+
|
|
406
|
+
if rows:
|
|
407
|
+
row = rows[0]
|
|
408
|
+
for db_col in fields_to_fetch:
|
|
409
|
+
model_field = db_projection_reverse.get(db_col, db_col)
|
|
410
|
+
field_object = self.model._meta.fields_map[model_field]
|
|
411
|
+
raw_value = row.get(db_col)
|
|
412
|
+
setattr(instance, model_field, field_object.to_python_value(raw_value))
|
|
413
|
+
|
|
414
|
+
def get_update_sql(
|
|
415
|
+
self,
|
|
416
|
+
update_fields: Iterable[str] | None,
|
|
417
|
+
expressions: dict[str, Expression] | None,
|
|
418
|
+
) -> str:
|
|
419
|
+
"""
|
|
420
|
+
Generates the SQL for updating a model depending on provided update_fields.
|
|
421
|
+
Result is cached for performance.
|
|
422
|
+
"""
|
|
423
|
+
key = ",".join(update_fields) if update_fields else ""
|
|
424
|
+
if not expressions and key in self.update_cache:
|
|
425
|
+
return self.update_cache[key]
|
|
426
|
+
expressions = expressions or {}
|
|
427
|
+
table = self.model._meta.basetable
|
|
428
|
+
query = self.db.query_class.update(table)
|
|
429
|
+
parameter_idx = 0
|
|
430
|
+
for field in update_fields or self.model._meta.fields_db_projection.keys():
|
|
431
|
+
db_column = self.model._meta.fields_db_projection[field]
|
|
432
|
+
field_object = self.model._meta.fields_map[field]
|
|
433
|
+
if field_object.generated:
|
|
434
|
+
if update_fields:
|
|
435
|
+
raise OperationalError(f"Can't update generated field {field}")
|
|
436
|
+
continue
|
|
437
|
+
if not field_object.pk:
|
|
438
|
+
if field not in expressions:
|
|
439
|
+
query = query.set(db_column, self.parameter(parameter_idx))
|
|
440
|
+
parameter_idx += 1
|
|
441
|
+
else:
|
|
442
|
+
value = (
|
|
443
|
+
expressions[field]
|
|
444
|
+
.resolve(
|
|
445
|
+
ResolveContext(
|
|
446
|
+
model=self.model,
|
|
447
|
+
table=table,
|
|
448
|
+
annotations={},
|
|
449
|
+
custom_filters={},
|
|
450
|
+
)
|
|
451
|
+
)
|
|
452
|
+
.term
|
|
453
|
+
)
|
|
454
|
+
query = query.set(db_column, value)
|
|
455
|
+
|
|
456
|
+
query = query.where(table[self.model._meta.db_pk_column] == self.parameter(parameter_idx))
|
|
457
|
+
|
|
458
|
+
sql = query.get_sql()
|
|
459
|
+
if not expressions:
|
|
460
|
+
self.update_cache[key] = sql
|
|
461
|
+
return sql
|
|
462
|
+
|
|
463
|
+
async def execute_update(
|
|
464
|
+
self, instance: type[Model] | Model, update_fields: Iterable[str] | None
|
|
465
|
+
) -> int:
|
|
466
|
+
|
|
467
|
+
user_specified = update_fields is not None
|
|
468
|
+
source_fields: list[str] = (
|
|
469
|
+
list(update_fields)
|
|
470
|
+
if update_fields is not None
|
|
471
|
+
else list(self.model._meta.fields_db_projection.keys())
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
effective_fields: list[str] = []
|
|
475
|
+
for field in source_fields:
|
|
476
|
+
field_obj = self.model._meta.fields_map[field]
|
|
477
|
+
if field_obj.pk:
|
|
478
|
+
if user_specified:
|
|
479
|
+
raise OperationalError(
|
|
480
|
+
f"Can't update pk field, use `{self.model.__name__}.create` instead."
|
|
481
|
+
)
|
|
482
|
+
continue
|
|
483
|
+
if field_obj.generated:
|
|
484
|
+
if user_specified:
|
|
485
|
+
raise OperationalError(f"Can't update generated field {field}")
|
|
486
|
+
continue
|
|
487
|
+
instance_field = getattr(instance, field)
|
|
488
|
+
if isinstance(instance_field, DatabaseDefault):
|
|
489
|
+
continue
|
|
490
|
+
effective_fields.append(field)
|
|
491
|
+
|
|
492
|
+
if not effective_fields:
|
|
493
|
+
return 0
|
|
494
|
+
|
|
495
|
+
values = []
|
|
496
|
+
expressions = {}
|
|
497
|
+
for field in effective_fields:
|
|
498
|
+
instance_field = getattr(instance, field)
|
|
499
|
+
if isinstance(instance_field, Expression):
|
|
500
|
+
expressions[field] = instance_field
|
|
501
|
+
else:
|
|
502
|
+
field_obj = self.model._meta.fields_map[field]
|
|
503
|
+
values.append(field_obj.to_db_value(instance_field, instance))
|
|
504
|
+
|
|
505
|
+
values.append(self.model._meta.pk.to_db_value(instance.pk, instance))
|
|
506
|
+
return (
|
|
507
|
+
await self.db.execute_query(self.get_update_sql(effective_fields, expressions), values)
|
|
508
|
+
)[0]
|
|
509
|
+
|
|
510
|
+
async def execute_delete(self, instance: type[Model] | Model) -> int:
|
|
511
|
+
return (
|
|
512
|
+
await self.db.execute_query(
|
|
513
|
+
self.delete_query, [self.model._meta.pk.to_db_value(instance.pk, instance)]
|
|
514
|
+
)
|
|
515
|
+
)[0]
|
|
516
|
+
|
|
517
|
+
async def _prefetch_reverse_relation(
|
|
518
|
+
self,
|
|
519
|
+
instance_list: Iterable[Model],
|
|
520
|
+
field: str,
|
|
521
|
+
related_query: tuple[str | None, QuerySet],
|
|
522
|
+
) -> Iterable[Model]:
|
|
523
|
+
to_attr, related_queryset = related_query
|
|
524
|
+
related_objects_for_fetch: dict[str, list] = {}
|
|
525
|
+
related_field: BackwardFKRelation = self.model._meta.fields_map[field] # type: ignore
|
|
526
|
+
related_field_name = related_field.to_field_instance.model_field_name
|
|
527
|
+
relation_field = related_field.relation_field
|
|
528
|
+
|
|
529
|
+
for instance in instance_list:
|
|
530
|
+
if relation_field not in related_objects_for_fetch:
|
|
531
|
+
related_objects_for_fetch[relation_field] = []
|
|
532
|
+
related_objects_for_fetch[relation_field].append(
|
|
533
|
+
instance._meta.fields_map[related_field_name].to_db_value(
|
|
534
|
+
getattr(instance, related_field_name), instance
|
|
535
|
+
)
|
|
536
|
+
)
|
|
537
|
+
|
|
538
|
+
related_queryset.resolve_ordering(
|
|
539
|
+
related_queryset.model, related_queryset.model._meta.basetable, [], {}
|
|
540
|
+
)
|
|
541
|
+
related_object_list = await related_queryset._filter_by_kwargs(
|
|
542
|
+
**{f"{k}__in": v for k, v in related_objects_for_fetch.items()}
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
related_object_map: dict[str, list] = {}
|
|
546
|
+
for entry in related_object_list:
|
|
547
|
+
object_id = getattr(entry, relation_field)
|
|
548
|
+
if object_id in related_object_map:
|
|
549
|
+
related_object_map[object_id].append(entry)
|
|
550
|
+
else:
|
|
551
|
+
related_object_map[object_id] = [entry]
|
|
552
|
+
for instance in instance_list:
|
|
553
|
+
relation_container = getattr(instance, field)
|
|
554
|
+
relation_container._set_result_for_query(
|
|
555
|
+
related_object_map.get(getattr(instance, related_field_name), []),
|
|
556
|
+
to_attr,
|
|
557
|
+
)
|
|
558
|
+
return instance_list
|
|
559
|
+
|
|
560
|
+
async def _prefetch_reverse_o2o_relation(
|
|
561
|
+
self,
|
|
562
|
+
instance_list: Iterable[Model],
|
|
563
|
+
field: str,
|
|
564
|
+
related_query: tuple[str | None, QuerySet],
|
|
565
|
+
) -> Iterable[Model]:
|
|
566
|
+
to_attr, related_queryset = related_query
|
|
567
|
+
related_objects_for_fetch: dict[str, list] = {}
|
|
568
|
+
related_field: BackwardOneToOneRelation = self.model._meta.fields_map[field] # type: ignore
|
|
569
|
+
related_field_name = related_field.to_field_instance.model_field_name
|
|
570
|
+
relation_field = related_field.relation_field
|
|
571
|
+
|
|
572
|
+
for instance in instance_list:
|
|
573
|
+
if relation_field not in related_objects_for_fetch:
|
|
574
|
+
related_objects_for_fetch[relation_field] = []
|
|
575
|
+
related_objects_for_fetch[relation_field].append(
|
|
576
|
+
instance._meta.fields_map[related_field_name].to_db_value(
|
|
577
|
+
getattr(instance, related_field_name), instance
|
|
578
|
+
)
|
|
579
|
+
)
|
|
580
|
+
|
|
581
|
+
related_object_list = await related_queryset._filter_by_kwargs(
|
|
582
|
+
**{f"{k}__in": v for k, v in related_objects_for_fetch.items()}
|
|
583
|
+
)
|
|
584
|
+
|
|
585
|
+
related_object_map = {}
|
|
586
|
+
for entry in related_object_list:
|
|
587
|
+
object_id = getattr(entry, relation_field)
|
|
588
|
+
related_object_map[object_id] = entry
|
|
589
|
+
|
|
590
|
+
for instance in instance_list:
|
|
591
|
+
obj = related_object_map.get(getattr(instance, related_field_name))
|
|
592
|
+
setattr(
|
|
593
|
+
instance,
|
|
594
|
+
f"_{field}",
|
|
595
|
+
obj,
|
|
596
|
+
)
|
|
597
|
+
if to_attr:
|
|
598
|
+
setattr(instance, to_attr, obj)
|
|
599
|
+
return instance_list
|
|
600
|
+
|
|
601
|
+
async def _prefetch_m2m_relation(
|
|
602
|
+
self,
|
|
603
|
+
instance_list: Iterable[MODEL],
|
|
604
|
+
field: str,
|
|
605
|
+
related_query: tuple[str | None, QuerySet],
|
|
606
|
+
) -> Iterable[MODEL]:
|
|
607
|
+
to_attr, related_queryset = related_query
|
|
608
|
+
pk_field = self.model._meta.pk
|
|
609
|
+
instance_id_set: set = {
|
|
610
|
+
pk_field.to_db_value(instance.pk, instance) for instance in instance_list
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
field_object: ManyToManyFieldInstance = self.model._meta.fields_map[field] # type: ignore
|
|
614
|
+
|
|
615
|
+
related_pk_field = related_queryset.model._meta.pk
|
|
616
|
+
related_pk_field_name = related_pk_field.model_field_name
|
|
617
|
+
fields_for_select = related_queryset._fields_for_select
|
|
618
|
+
if fields_for_select and related_pk_field_name not in fields_for_select:
|
|
619
|
+
related_queryset = related_queryset.only(
|
|
620
|
+
*related_queryset._fields_for_select, related_pk_field_name
|
|
621
|
+
)
|
|
622
|
+
through_table = Table(field_object.through, schema=field_object.through_schema)
|
|
623
|
+
backward_key = field_object.backward_key
|
|
624
|
+
forward_key = field_object.forward_key
|
|
625
|
+
backward_field = through_table[backward_key]
|
|
626
|
+
forward_field = through_table[forward_key]
|
|
627
|
+
subquery = (
|
|
628
|
+
self.db.query_class.from_(through_table)
|
|
629
|
+
.select(backward_field, forward_field)
|
|
630
|
+
.where(backward_field.isin(instance_id_set))
|
|
631
|
+
)
|
|
632
|
+
_, through_rows = await self.db.execute_query(*subquery.get_parameterized_sql())
|
|
633
|
+
if not through_rows:
|
|
634
|
+
for instance in instance_list:
|
|
635
|
+
getattr(instance, field)._set_result_for_query([], to_attr)
|
|
636
|
+
return instance_list
|
|
637
|
+
|
|
638
|
+
reverse_map: dict = {}
|
|
639
|
+
for row in through_rows:
|
|
640
|
+
forward_key_value = related_pk_field.to_python_value(row[forward_key])
|
|
641
|
+
backward_key_value = pk_field.to_python_value(row[backward_key])
|
|
642
|
+
reverse_map.setdefault(forward_key_value, []).append(backward_key_value)
|
|
643
|
+
related_objects = await related_queryset._filter_by_kwargs(pk__in=list(reverse_map))
|
|
644
|
+
relation_map: dict = {}
|
|
645
|
+
for related_object in related_objects:
|
|
646
|
+
for instance_pk in reverse_map.get(related_object.pk, []):
|
|
647
|
+
relation_map.setdefault(instance_pk, []).append(related_object)
|
|
648
|
+
|
|
649
|
+
for instance in instance_list:
|
|
650
|
+
relation_container = getattr(instance, field)
|
|
651
|
+
relation_container._set_result_for_query(relation_map.get(instance.pk, []), to_attr)
|
|
652
|
+
return instance_list
|
|
653
|
+
|
|
654
|
+
async def _prefetch_direct_relation(
|
|
655
|
+
self,
|
|
656
|
+
instance_list: Iterable[Model],
|
|
657
|
+
field: str,
|
|
658
|
+
related_query: tuple[str | None, QuerySet],
|
|
659
|
+
) -> Iterable[Model]:
|
|
660
|
+
to_attr, related_queryset = related_query
|
|
661
|
+
related_objects_for_fetch: dict[str, list] = {}
|
|
662
|
+
relation_key_field = f"{field}_id"
|
|
663
|
+
model_to_field: dict[type[Model], str] = {}
|
|
664
|
+
for instance in instance_list:
|
|
665
|
+
if (value := getattr(instance, relation_key_field)) is not None:
|
|
666
|
+
if (model_cls := instance.__class__) in model_to_field:
|
|
667
|
+
key = model_to_field[model_cls]
|
|
668
|
+
else:
|
|
669
|
+
related_field = cast(RelationalField, instance._meta.fields_map[field])
|
|
670
|
+
model_to_field[model_cls] = key = related_field.to_field
|
|
671
|
+
if key not in related_objects_for_fetch:
|
|
672
|
+
related_objects_for_fetch[key] = []
|
|
673
|
+
if value not in (values := related_objects_for_fetch[key]):
|
|
674
|
+
values.append(value)
|
|
675
|
+
else:
|
|
676
|
+
setattr(instance, field, None)
|
|
677
|
+
|
|
678
|
+
if related_objects_for_fetch:
|
|
679
|
+
conditions: dict[str, Any] = {}
|
|
680
|
+
for k, v in related_objects_for_fetch.items():
|
|
681
|
+
if len(v) == 1:
|
|
682
|
+
v = v[0]
|
|
683
|
+
else:
|
|
684
|
+
k += "__in"
|
|
685
|
+
conditions[k] = v
|
|
686
|
+
related_object_list = await related_queryset._filter_by_kwargs(**conditions)
|
|
687
|
+
if len(model_to_field) > 1:
|
|
688
|
+
related_object_map = {
|
|
689
|
+
getattr(obj, model_to_field[obj.__class__]): obj for obj in related_object_list
|
|
690
|
+
}
|
|
691
|
+
else:
|
|
692
|
+
key = next(iter(model_to_field.values()))
|
|
693
|
+
related_object_map = {getattr(obj, key): obj for obj in related_object_list}
|
|
694
|
+
for instance in instance_list:
|
|
695
|
+
obj = related_object_map.get(getattr(instance, relation_key_field))
|
|
696
|
+
setattr(instance, field, obj)
|
|
697
|
+
if to_attr:
|
|
698
|
+
setattr(instance, to_attr, obj)
|
|
699
|
+
return instance_list
|
|
700
|
+
|
|
701
|
+
def _make_prefetch_queries(self) -> None:
|
|
702
|
+
for field_name, forwarded_prefetches in self.prefetch_map.items():
|
|
703
|
+
to_attr = None
|
|
704
|
+
if field_name in self._prefetch_queries:
|
|
705
|
+
to_attr, related_query = self._prefetch_queries[field_name][0]
|
|
706
|
+
else:
|
|
707
|
+
relation_field = self.model._meta.fields_map[field_name]
|
|
708
|
+
related_model: type[Model] = relation_field.related_model # type: ignore
|
|
709
|
+
related_query = related_model.all().using_db(self.db)
|
|
710
|
+
related_query.query = copy(related_query.model._meta.basequery) # type:ignore[assignment]
|
|
711
|
+
if forwarded_prefetches:
|
|
712
|
+
related_query = related_query.prefetch_related(*forwarded_prefetches)
|
|
713
|
+
self._prefetch_queries.setdefault(field_name, []).append((to_attr, related_query))
|
|
714
|
+
|
|
715
|
+
async def _do_prefetch(
|
|
716
|
+
self,
|
|
717
|
+
instance_id_list: Iterable[Model],
|
|
718
|
+
field: str,
|
|
719
|
+
related_query: tuple[str | None, QuerySet],
|
|
720
|
+
) -> Iterable[Model]:
|
|
721
|
+
if field in self.model._meta.backward_fk_fields:
|
|
722
|
+
return await self._prefetch_reverse_relation(instance_id_list, field, related_query)
|
|
723
|
+
|
|
724
|
+
if field in self.model._meta.backward_o2o_fields:
|
|
725
|
+
return await self._prefetch_reverse_o2o_relation(instance_id_list, field, related_query)
|
|
726
|
+
|
|
727
|
+
if field in self.model._meta.m2m_fields:
|
|
728
|
+
return await self._prefetch_m2m_relation(instance_id_list, field, related_query)
|
|
729
|
+
return await self._prefetch_direct_relation(instance_id_list, field, related_query)
|
|
730
|
+
|
|
731
|
+
async def _execute_prefetch_queries(self, instance_list: Iterable[Model]) -> Iterable[Model]:
|
|
732
|
+
if instance_list and (self.prefetch_map or self._prefetch_queries):
|
|
733
|
+
self._make_prefetch_queries()
|
|
734
|
+
prefetch_tasks = []
|
|
735
|
+
for field, related_queries in self._prefetch_queries.items():
|
|
736
|
+
for related_query in related_queries:
|
|
737
|
+
prefetch_tasks.append(self._do_prefetch(instance_list, field, related_query))
|
|
738
|
+
await asyncio.gather(*prefetch_tasks)
|
|
739
|
+
|
|
740
|
+
return instance_list
|
|
741
|
+
|
|
742
|
+
async def fetch_for_list(self, instance_list: Iterable[Model], *args: str) -> Iterable[Model]:
|
|
743
|
+
self.prefetch_map = {}
|
|
744
|
+
for relation in args:
|
|
745
|
+
first_level_field, __, forwarded_prefetch = relation.partition("__")
|
|
746
|
+
if first_level_field not in self.model._meta.fetch_fields:
|
|
747
|
+
raise OperationalError(
|
|
748
|
+
f"relation {first_level_field} for {self.model._meta.db_table} not found"
|
|
749
|
+
)
|
|
750
|
+
|
|
751
|
+
if first_level_field not in self.prefetch_map:
|
|
752
|
+
self.prefetch_map[first_level_field] = set()
|
|
753
|
+
|
|
754
|
+
if forwarded_prefetch:
|
|
755
|
+
self.prefetch_map[first_level_field].add(forwarded_prefetch)
|
|
756
|
+
|
|
757
|
+
await self._execute_prefetch_queries(instance_list)
|
|
758
|
+
return instance_list
|
|
759
|
+
|
|
760
|
+
@classmethod
|
|
761
|
+
def get_overridden_filter_func(
|
|
762
|
+
cls, filter_func: Callable, filter_info: FilterInfoDict | None = None
|
|
763
|
+
) -> Callable | None:
|
|
764
|
+
return cls.FILTER_FUNC_OVERRIDE.get(filter_func)
|