File tree 1 file changed +7
-2
lines changed
1 file changed +7
-2
lines changed Original file line number Diff line number Diff line change @@ -823,17 +823,22 @@ class QuestSerializer(serializers.ModelSerializer):
823
823
824
824
Be aware that using included resources without any form of prefetching ** WILL HURT PERFORMANCE** as it will introduce m\* (n+1) queries.
825
825
826
- A viewset helper was designed to allow for greater flexibility and it is automatically available when subclassing
826
+ A viewset helper was designed to allow for greater flexibility and it is automatically available when subclassing.
827
+ You can also define your custom queryset for ` select ` or ` prefetch ` related for each ` include ` that comes from the url.
828
+ It has a priority over automatically added preloads.
827
829
` rest_framework_json_api.views.ModelViewSet ` :
828
830
``` python
829
831
from rest_framework_json_api import views
830
832
831
833
# When MyViewSet is called with ?include=author it will dynamically prefetch author and author.bio
832
834
class MyViewSet (views .ModelViewSet ):
833
835
queryset = Book.objects.all()
836
+ select_for_includes = {
837
+ ' author' : [' author__bio' ],
838
+ }
834
839
prefetch_for_includes = {
835
840
' __all__' : [],
836
- ' author ' : [' author ' , ' author__bio ' ],
841
+ ' all_authors ' : [Prefetch( ' all_authors ' , queryset = Author.objects.select_related( ' bio ' )) ],
837
842
' category.section' : [' category' ]
838
843
}
839
844
```
You can’t perform that action at this time.
0 commit comments