tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.41k stars 266 forks source link

Using api view function django rest framework with dispatcher module #1023

Open matiasAS opened 1 year ago

matiasAS commented 1 year ago

Hello, my endpoint structure in django is the following: image

dispatcher function:

image

and @extend_schema :

image

I need you to acknowledge that in my response status code 200 is

image

how do i do that? please help

Regards Matias

tfranzel commented 1 year ago

screenshots 2 and 3 are identical. what is your @extend_schema()?

you may want to use this:

@extend_schema(responses=inline_serializer(
    name='BusinessSerializer',
    fields={
        'business': serializers.CharField(),
    }
))
#OR 
@extend_schema(responses={200:inline_serializer(...)})

https://drf-spectacular.readthedocs.io/en/latest/customization.html?highlight=inline_serializer#step-2-extend-schema

https://github.com/tfranzel/drf-spectacular/blob/e0f749e9857d938693311af67bd575800cbc6aab/tests/test_regressions.py#L1070

matiasAS commented 1 year ago

@tfranzel sorry, @extend_schema is:

image

I have made those changes, but what I want to say is that apparently as my response of type JsonResponse comes from the dispatcher, drf-spectacular does not recognize it and asks me to use GenericAPIView or ApiView classes, but I prefer api view function Regards

tfranzel commented 1 year ago

You can use ApiView for everything. No problem there. the @extend_schema() decorator must be above @api_view()

The warning is because some information is missing and spectacular is unable to guess it. When you provide that information manually, it will work.

matiasAS commented 1 year ago

@tfranzel and these errors: ...../api/v1/views.py: Error [create_irrigation_sector_group]: unable to guess serializer. This is graceful fallback handling for APIViews. Consider using GenericAPIView as view base class, if view is under your control. Either way you may want to add a serializer_class (or method). Ignoring view for now.

what is the solution?

tfranzel commented 1 year ago

every time you use ApiView or @api_view, you must provide all the info through @extend_schema which would otherwise be available naturally in a ViewSet.

You did good with the id param and the responses. That should be fine. Without knowing more, I can only guess this is a POST and spectacular attempts to retrieve the request serializer and fails there. try also adding that one with @extend_schema(..., request=...).

P.S That many=False does nothing like that.

Tuhin-thinks commented 3 months ago

@matiasAS

You have to add request=SomeSerializer in your @extend_schema decorator.

image