How can I specify a parameter for POST requests when using APIView with django-rest-swagger

In the latest version on Django REST Swagger (2.1.0), YAML docstrings are deprecated. I can not make out the parameters of the POST request.

Here is my look

class UserAuthenticationView(APIView):
    def post(self, request, *args, **kwargs):
        serializer = UserAuthenticationSerializer(data=self.request.data)
        if serializer.is_valid():
            user = serializer.validated_data['user']
            return Response({'token': user.auth_token.key}, status=status.HTTP_200_OK)

        return Response(serializer.errors, status=status.HTTP_401_UNAUTHORIZED)

Here is my serializer

class UserAuthenticationSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, attrs):
        username = attrs.get('username')
        password = attrs.get('password')

        if username and password:
            user = authenticate(username=username, password=password)
            if user:

                if not user.is_active:
                    msg = 'User account is disabled.'
                    raise serializers.ValidationError(msg, code='authorization')

            else:
                msg = 'Unable to log in with provided credentials.'
                raise serializers.ValidationError(msg, code='authorization')

        else:
            msg = 'Must include "username" and "password".'
            raise serializers.ValidationError(msg, code='authorization')

        attrs['user'] = user
        return attrs

This is what I get in my generated enter image description here

I do not get a form with fields for POST data. How do i get this?

+5
source share
3 answers

This rest of the structure receives the code of the scheme (part of it):

def get_serializer_fields(self, path, method, view):
    """
    Return a list of `coreapi.Field` instances corresponding to any
    request body input, as determined by the serializer class.
    """
    if method not in ('PUT', 'PATCH', 'POST'):
        return []

    if not hasattr(view, 'get_serializer'):
        return []

    serializer = view.get_serializer()

    if isinstance(serializer, serializers.ListSerializer):
        return [
            coreapi.Field(
                name='data',
                location='body',
                required=True,
                type='array'
            )
        ]
...

As you can see - it should work if you define a method get_serializeron your view - which returns UserAuthenticationSerializer.

- EDIT -

Forget: Happy coding.

+2

django-rest-swagger rest_framework.schemas.SchemaGenerator , SchemaGenerator get_serializer_fields . get_serializer_fields , get_serializer . GenericAPIView get_serializer, .

GenericAPIView, APIView. serializer_class

from rest_framework.generics import GenericAPIView 

class UserAuthenticationView(GenericAPIView):

    serializer_class = UserAuthenticationSerializer

    def post(self, request, *args, **kwargs):
        serializer = UserAuthenticationSerializer(data=self.request.data)
        if serializer.is_valid():
            user = serializer.validated_data['user']
            return Response({'token': user.auth_token.key}, status=status.HTTP_200_OK)    
        return Response(serializer.errors, status=status.HTTP_401_UNAUTHORIZED)
+12

ViewSet django-rest-swagger == 2.2.0:

from rest_framework import viewsets
from rest_framework.schemas import AutoSchema
from rest_framework.compat import coreapi, coreschema
from rest_framework.decorators import action


class DeviceViewSchema(AutoSchema):
    """
    Schema customizations for DeviceViewSet
    """

    def get_manual_fields(self, path, method):
        extra_fields = []
        if path.endswith('/send_command/'):
            extra_fields = [
                coreapi.Field(
                    "command",
                    required=True,
                    location="form",
                    schema=coreschema.String()
                ),
                coreapi.Field(
                    "params",
                    required=False,
                    location="form",
                    schema=coreschema.String()
                ),
            ]
        manual_fields = super().get_manual_fields(path, method)
        return manual_fields + extra_fields


class DeviceViewSet(viewsets.ViewSet):
    lookup_field = 'channel'
    lookup_value_regex = '[\w-]+'

    schema = DeviceViewSchema()

    @action(methods=['post'], detail=True, url_name='send_command')
    def send_command(self, request, channel):
        """
        Send command to device

        Parameters:
        - command: string
        - params: string (JSON encoded list or dict)
        """
        ...

:

enter image description here

0
source

Source: https://habr.com/ru/post/1663529/


All Articles