Skip to content

Commit f73c6d9

Browse files
committed
prep _postprocess_django_query for potential API changes
1 parent 2b8d036 commit f73c6d9

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

src/django_idom/hooks.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ def execute_query() -> None:
120120

121121
try:
122122
# Run the initial query
123-
query_options._data = query_options.func(*args, **kwargs)
123+
data = query_options.func(*args, **kwargs)
124124

125125
# Use a custom postprocessor, if provided
126126
if query_options.postprocessor:
127127
query_options.postprocessor(query_options)
128128

129129
# Use the default postprocessor
130130
else:
131-
_postprocess_django_query(query_options)
131+
_postprocess_django_query(data, query_options.postprocessor_options)
132132
except Exception as e:
133133
set_data(None)
134134
set_loading(False)
@@ -140,7 +140,7 @@ def execute_query() -> None:
140140
finally:
141141
set_should_execute(False)
142142

143-
set_data(query_options._data)
143+
set_data(data)
144144
set_loading(False)
145145
set_error(None)
146146

@@ -200,49 +200,35 @@ def reset() -> None:
200200
return Mutation(call, loading, error, reset)
201201

202202

203-
def _postprocess_django_query(fetch_cls: QueryOptions) -> None:
203+
def _postprocess_django_query(data: QuerySet | Model, options: dict[str, Any]) -> None:
204204
"""Recursively fetch all fields within a `Model` or `QuerySet` to ensure they are not performed lazily.
205205
206206
Some behaviors can be modified through `query_options` attributes."""
207-
data = fetch_cls._data
208207

209-
# `QuerySet`, which is effectively a list of `Model` instances
208+
# `QuerySet`, which is an iterable of `Model`/`QuerySet` instances
210209
# https://github.com/typeddjango/django-stubs/issues/704
211210
if isinstance(data, QuerySet): # type: ignore[misc]
212211
for model in data:
213-
_postprocess_django_query(
214-
QueryOptions(
215-
func=fetch_cls.func,
216-
_data=model,
217-
postprocessor_options=fetch_cls.postprocessor_options,
218-
)
219-
)
212+
_postprocess_django_query(model, options)
220213

221214
# `Model` instances
222215
elif isinstance(data, Model):
223-
prefetch_fields = []
216+
prefetch_fields: list[str] = []
224217
for field in data._meta.get_fields():
225218
# `ForeignKey` relationships will cause an `AttributeError`
226219
# This is handled within the `ManyToOneRel` conditional below.
227220
with contextlib.suppress(AttributeError):
228221
getattr(data, field.name)
229222

230-
if (
231-
fetch_cls.postprocessor_options.get("many_to_one", None)
232-
and type(field) == ManyToOneRel
233-
):
223+
if options.get("many_to_one", None) and type(field) == ManyToOneRel:
234224
prefetch_fields.append(f"{field.name}_set")
235225

236-
elif fetch_cls.postprocessor_options.get(
237-
"many_to_many", None
238-
) and isinstance(field, ManyToManyField):
226+
elif options.get("many_to_many", None) and isinstance(
227+
field, ManyToManyField
228+
):
239229
prefetch_fields.append(field.name)
240230
_postprocess_django_query(
241-
QueryOptions(
242-
func=fetch_cls.func,
243-
_data=getattr(data, field.name).get_queryset(),
244-
postprocessor_options=fetch_cls.postprocessor_options,
245-
)
231+
getattr(data, field.name).get_queryset(), options
246232
)
247233

248234
if prefetch_fields:

src/django_idom/types.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ def __call__(self, *args, **kwargs):
6363
func: Callable
6464
"""Callable that fetches ORM object(s)."""
6565

66-
_data: Any = None
67-
"""The results of a fetch operation.
68-
This is only intended to be set automatically by the `use_query` hook."""
69-
7066
postprocessor_options: dict[str, Any] = field(
7167
default_factory=lambda: {"many_to_many": False, "many_to_one": False}
7268
)

0 commit comments

Comments
 (0)