@@ -120,15 +120,15 @@ def execute_query() -> None:
120
120
121
121
try :
122
122
# Run the initial query
123
- query_options . _data = query_options .func (* args , ** kwargs )
123
+ data = query_options .func (* args , ** kwargs )
124
124
125
125
# Use a custom postprocessor, if provided
126
126
if query_options .postprocessor :
127
127
query_options .postprocessor (query_options )
128
128
129
129
# Use the default postprocessor
130
130
else :
131
- _postprocess_django_query (query_options )
131
+ _postprocess_django_query (data , query_options . postprocessor_options )
132
132
except Exception as e :
133
133
set_data (None )
134
134
set_loading (False )
@@ -140,7 +140,7 @@ def execute_query() -> None:
140
140
finally :
141
141
set_should_execute (False )
142
142
143
- set_data (query_options . _data )
143
+ set_data (data )
144
144
set_loading (False )
145
145
set_error (None )
146
146
@@ -200,49 +200,35 @@ def reset() -> None:
200
200
return Mutation (call , loading , error , reset )
201
201
202
202
203
- def _postprocess_django_query (fetch_cls : QueryOptions ) -> None :
203
+ def _postprocess_django_query (data : QuerySet | Model , options : dict [ str , Any ] ) -> None :
204
204
"""Recursively fetch all fields within a `Model` or `QuerySet` to ensure they are not performed lazily.
205
205
206
206
Some behaviors can be modified through `query_options` attributes."""
207
- data = fetch_cls ._data
208
207
209
- # `QuerySet`, which is effectively a list of `Model` instances
208
+ # `QuerySet`, which is an iterable of `Model`/`QuerySet ` instances
210
209
# https://github.com/typeddjango/django-stubs/issues/704
211
210
if isinstance (data , QuerySet ): # type: ignore[misc]
212
211
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 )
220
213
221
214
# `Model` instances
222
215
elif isinstance (data , Model ):
223
- prefetch_fields = []
216
+ prefetch_fields : list [ str ] = []
224
217
for field in data ._meta .get_fields ():
225
218
# `ForeignKey` relationships will cause an `AttributeError`
226
219
# This is handled within the `ManyToOneRel` conditional below.
227
220
with contextlib .suppress (AttributeError ):
228
221
getattr (data , field .name )
229
222
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 :
234
224
prefetch_fields .append (f"{ field .name } _set" )
235
225
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
+ ):
239
229
prefetch_fields .append (field .name )
240
230
_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
246
232
)
247
233
248
234
if prefetch_fields :
0 commit comments