-
Notifications
You must be signed in to change notification settings - Fork 300
-
At the moment, we have select_for_includes and prefetch_for_includes for dynamic prefetching and selecting. I was wondering if there is anything similar for the fields[resource] parameter (I believe there is not) since I'm adding some metadata to my resources:
{
...,
"meta": {
"user": {
"liked": true,
"saved": false
}
}
}In this case, I make a new DB query on every resource that the API returns, since this requires checking if the resource is in the user's library. This query can be omitted if the user is not logged in or the request has fields[resource] without user as a query parameter, but I'd like to prefetch the related resources only if the field is present.
ATM, I need to decide whether to prefetch all the users or none of them and make an extra query. Is there any way to accomplish this or not?
My suggestion in case this is not yet possible is to add a prefetch_for_fields and select_for_fields which would work exactly as the current prefetch_for_fields and select_for_fields.
Note: After writing this I realized I can avoid making a new DB query using values_list.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Prefetching is a topic which needs to be improved within DJA however it is also not an easy task. Best take a look at discussion #921 to see what approaches are being discussed, and there are also some code bits and PRs referenced.
My feeling is that we should make auto prefetching as best as possible. But when it cannot cover what is needed, it should be possible to simply disable it and a DJA user can then do their own optimization within their code (like overwriting get_queryset and do the prefetches there.)
Beta Was this translation helpful? Give feedback.