简体   繁体   中英

Incorrect schema in DRF documentation for custom ViewSet action

I have such ViewSet:

class CarViewSet(ModelViewSet):
"""
Работа пользователя с его машинами.
"""
serializer_class = CarSerializer
permission_classes = [IsUser]
filter_fields = '__all__'
ordering_fields = '__all__'

def perform_create(self, serializer):
    serializer.save(user=self.request.user.useraccount)

def get_queryset(self):
    return Car.objects.filter(user=self.request.user)

@action(methods=['POST'], detail=True)
def set_default(self, request, pk=None):
    """
    Установить указанную машину по умолчанию
    """
    car = get_object_or_404(self.get_queryset(), pk=pk)
    car.is_default = True
    car.save()
    return Response()

And in rest_framework.documentation page I see such form for a "set_default" action: 在此处输入图片说明

The problem is "set_default" actually don't need to provide all of these fields, it requires(and uses) only id!

What can I do with this?

If you don't need to send these data in body, then why don't you use get method?

@action(methods=['GET'], detail=True)
def set_default(self, request, pk=None):
    """
    Установить указанную машину по умолчанию
    """
    car = get_object_or_404(self.get_queryset(), pk=pk)
    car.is_default = True
    car.save()
    return Response({"updated":True})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM