How to use api_view with drf_spectacular

58 views Asked by At

I follow this link(and many other link): https://drf-spectacular.readthedocs.io/en/latest/faq.html#how-to-correctly-annotate-function-based-views-that-use-api-view but docs always show “No operations defined in spec!”

# url.py
urlpatterns = [
    path("schema/", SpectacularAPIView.as_view(), name="schema"),
    path("docs/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"),
    path('pi/', FeedView.as_view()),
]

# views.py
class FeedView(APIView):

    @extend_schema(responses=OpenApiTypes.FLOAT)
    @api_view(['GET'])
    def get(request, format=None):
        pass  # pragma: no cover

How can I resolve it? Thanks, attach sourcecode link https://firebasestorage.googleapis.com/v0/b/test-project-33489.appspot.com/o/Test%2Fmysite.zip?alt=media&token=b272288f-c743-4d44-8ad3-0a8b6977d265

1

There are 1 answers

0
Tony.Luo On

refer this link: https://drf-spectacular.readthedocs.io/en/latest/faq.html#i-get-an-empty-schema-or-endpoints-are-missing If you use

'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning'.

drf-spectacular will not contain unversioned endpoints, every endpoints must be versioned. But single function base view cannot be assigned to one namespace. So you should use include like:

path('api/', include(('polls.urls', 'polls'), namespace='v1')),

BTW,@api_view should below @extend_schema,pay attention to the order

@extend_schema(...)
@api_view(...)
def func(request):
    ...