File tree Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Expand file tree Collapse file tree 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):
823823
824824Be aware that using included resources without any form of prefetching ** WILL HURT PERFORMANCE** as it will introduce m\* (n+1) queries.
825825
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.
827829` rest_framework_json_api.views.ModelViewSet ` :
828830``` python
829831from rest_framework_json_api import views
830832
831833# When MyViewSet is called with ?include=author it will dynamically prefetch author and author.bio
832834class MyViewSet (views .ModelViewSet ):
833835 queryset = Book.objects.all()
836+ select_for_includes = {
837+ ' author' : [' author__bio' ],
838+ }
834839 prefetch_for_includes = {
835840 ' __all__' : [],
836- ' author ' : [' author ' , ' author__bio ' ],
841+ ' all_authors ' : [Prefetch( ' all_authors ' , queryset = Author.objects.select_related( ' bio ' )) ],
837842 ' category.section' : [' category' ]
838843 }
839844```
You can’t perform that action at this time.
0 commit comments