Skip to content

Commit

Permalink
Merge pull request #228 from jeffrey4l/master
Browse files Browse the repository at this point in the history
Fix KeyError exception for nested resource during generate swagger.
  • Loading branch information
alanjds authored Aug 26, 2021
2 parents 030ded8 + 124daa4 commit 06e2bcd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rest_framework_nested/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def get_queryset(self):
"""
queryset = super(NestedViewSetMixin, self).get_queryset()

if getattr(self, 'swagger_fake_view', False):
return queryset

orm_filters = {}
parent_lookup_kwargs = self._get_parent_lookup_kwargs()
for query_param, field_name in parent_lookup_kwargs.items():
Expand Down
11 changes: 11 additions & 0 deletions tests/test_viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.serializers import HyperlinkedModelSerializer, ModelSerializer
from rest_framework.viewsets import ModelViewSet
from rest_framework.reverse import reverse as drf_reverse
from rest_framework.schemas import generators

from rest_framework_nested.routers import NestedSimpleRouter
from rest_framework_nested.serializers import NestedHyperlinkedModelSerializer
Expand Down Expand Up @@ -210,3 +211,13 @@ def test_create_child_on_viewset_with_mixin(self):
kwargs={'pk': self.root_1.pk},
request=response.wsgi_request)
self.assertEqual(data['parent'], parent_url)

def test_get_queryset_for_children_resource(self):
gen = generators.BaseSchemaGenerator()
gen._initialise_endpoints()
for path, method, callback in gen.endpoints:
view = gen.create_view(callback, method)
# drf_yasg set swagger_fake_view attribute for all view
setattr(view, 'swagger_fake_view', True)
# no error message should be raised here
view.get_queryset()

0 comments on commit 06e2bcd

Please sign in to comment.