How to map a patch method over an action with detail=False in Django Rest Framework

I am building an API with Django and Django Rest Framework

I have the following endpoint:
host/products/ pointing to a ModelViewSet, so I have a CRUD working for the specified model.

Also, I define an extra action for a nested model called config with the following code:

@action(detail=False, methods=['get', 'post'])
def config(self, request, *args, **kwargs):

    if request.method == 'GET':
        return super().list(request, *args, **kwargs)

    elif request.method == 'POST':
        return super().create(request, *args, **kwargs)

The created URL is: host/products/config/

At this URL I can create and list objects in the second specified model

The problem is that I want to include PATCH and DELETE methods for the nested URL, I mean:
host/products/config/detail/

I try to do something like this:

@action(detail=True)
@config.mapping.patch
def update_config(self, request, *args, **kwargs):
    return super().update(request, *args, **kwargs)

But of course, it does not work...

How can I map details action over another action in the same viewset?

The complete code is the following:

class ProductViewSet(viewsets.ModelViewSet):

def get_permissions(self):
    ''' Assign permissions based on action. '''
    if self.action in ['suggestions']:
        permission_classes = [AllowAny]
    else:
        permission_classes = [AllowAny] # [IsAdminUser | IsDevUser]
    return [permission() for permission in permission_classes]

def get_queryset(self):
    ''' Return queryset based on action. '''
    if self.action == 'config':
        # Return product details
        return ProductDetails.objects.filter(active=True)
    else:
        # Return active products
        return Product.objects.filter(active=True)

def get_serializer_class(self):
    ''' Return serializer based on action. '''
    if self.action == 'suggestions':
        return SurveySerializer
    elif self.action == 'config':
        return ProductDetailModelSerializer
    else:
        return ProductModelSerializer

@action(detail=False, methods=['get', 'post'])
def config(self, request, *args, **kwargs):

    if request.method == 'GET':
        return super().list(request, *args, **kwargs)

    elif request.method == 'POST':
        return super().create(request, *args, **kwargs)

@action(detail=True)
@config.mapping.patch
def update_config(self, request, *args, **kwargs):
    return super().update(request, *args, **kwargs)
Back to Top