Different descriptions for methods in custom @action for routing

904 Views Asked by At

I did custom routable method with @action decorator for DRF ViewSet.

This rout implements multiple methods (GET, POST, DELETE). Is possible to have different descriptions for each method, or even to create different @extend_schema definitions for each method?

Uroš

2

There are 2 best solutions below

0
On

According to documentation link @<function_name>.mapping.delete will allow for splitting function in multiple methods (.delete for my case). Now it is trivial to properly document each method.

0
On

You can do this without the need for overriding the individual methods

@extend_schema_view(
    list=extend_schema(description='view list description'),
    retrieve=extend_schema(description='view retrieve description'),
    extended_action=extend_schema(description='view extended action description'),
    raw_action=extend_schema(description='view raw action description'),
)
class XViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
   ...

You can also use @extend_schema on any @action or regular method (e.g. retrieve, get). This also works for splits:

@extend_schema(request=UpdateSerializer)
@multi2.mapping.put
def multi2put(self, request, *args, **kwargs):
    ...