@@ -159,11 +159,11 @@ def execute_query() -> None:
159
159
160
160
# Use a custom postprocessor, if provided
161
161
if query_options .postprocessor :
162
- query_options .postprocessor (data , query_options )
162
+ query_options .postprocessor (data , ** query_options . postprocessor_options )
163
163
164
164
# Use the default postprocessor
165
165
else :
166
- _postprocess_django_query (data , query_options .postprocessor_options )
166
+ _postprocess_django_query (data , ** query_options .postprocessor_options )
167
167
except Exception as e :
168
168
set_data (None )
169
169
set_loading (False )
@@ -235,7 +235,9 @@ def reset() -> None:
235
235
return Mutation (call , loading , error , reset )
236
236
237
237
238
- def _postprocess_django_query (data : QuerySet | Model , options : dict [str , Any ]) -> None :
238
+ def _postprocess_django_query (
239
+ data : QuerySet | Model , / , many_to_many : bool = False , many_to_one : bool = False
240
+ ) -> None :
239
241
"""Recursively fetch all fields within a `Model` or `QuerySet` to ensure they are not performed lazily.
240
242
241
243
Some behaviors can be modified through `query_options` attributes."""
@@ -244,7 +246,11 @@ def _postprocess_django_query(data: QuerySet | Model, options: dict[str, Any]) -
244
246
# https://github.com/typeddjango/django-stubs/issues/704
245
247
if isinstance (data , QuerySet ): # type: ignore[misc]
246
248
for model in data :
247
- _postprocess_django_query (model , options )
249
+ _postprocess_django_query (
250
+ model ,
251
+ many_to_many = many_to_many ,
252
+ many_to_one = many_to_one ,
253
+ )
248
254
249
255
# `Model` instances
250
256
elif isinstance (data , Model ):
@@ -255,20 +261,26 @@ def _postprocess_django_query(data: QuerySet | Model, options: dict[str, Any]) -
255
261
with contextlib .suppress (AttributeError ):
256
262
getattr (data , field .name )
257
263
258
- if options . get ( " many_to_one" , False ) and type (field ) == ManyToOneRel :
264
+ if many_to_one and type (field ) == ManyToOneRel :
259
265
prefetch_fields .append (f"{ field .name } _set" )
260
266
261
- elif options .get ("many_to_many" , False ) and isinstance (
262
- field , ManyToManyField
263
- ):
267
+ elif many_to_many and isinstance (field , ManyToManyField ):
264
268
prefetch_fields .append (field .name )
265
269
_postprocess_django_query (
266
- getattr (data , field .name ).get_queryset (), options
270
+ getattr (data , field .name ).get_queryset (),
271
+ many_to_many = many_to_many ,
272
+ many_to_one = many_to_one ,
267
273
)
268
274
269
275
if prefetch_fields :
270
276
prefetch_related_objects ([data ], * prefetch_fields )
271
277
272
278
# Unrecognized type
273
279
else :
274
- raise ValueError (f"Expected a Model or QuerySet, got { data !r} " )
280
+ raise TypeError (
281
+ f"Django query postprocessor expected a Model or QuerySet, got { data !r} .\n "
282
+ "One of the following may have occurred:\n "
283
+ " - You are using a non-Django ORM.\n "
284
+ " - You are attempting to use `use_query` to fetch a non-ORM data.\n \n "
285
+ "If these situations seem correct, you may want to consider disabling the postprocessor via `QueryOptions`."
286
+ )
0 commit comments