A
A
Antonio Solo2021-05-03 13:24:43
Django Rest Framework
Antonio Solo, 2021-05-03 13:24:43

How to describe schema for DRF+swagger when data is passed as formdata?

have a view

@api_view(('POST',))
def user_login(request):
    """
        Апи для логина
    """
    username = request.data.get('user_login', '*************')
    password = request.data.get('password', '*************')

    CAB = UserphoneOrEmailBackend()
    user = CAB.authenticate(
        username=username,
        password=password
    )
    if user:  # Проверяем есть ли юзер в бд
        if user.is_active:  # Проверяем активен ли юзер
            login(request, user)

            return JsonResponse({'isAuth': 'true', 'id': user.id, 'session_key': request.session.session_key},
                                status=200)
        else:
            return JsonResponse({'error': 'аккаунт неактивен'}, status=500)
    else:
        return JsonResponse({'error': 'некорректные данные для входа'}, status=500)


swagger expectedly generates an empty form for it,

608fcd82d02c2288498727.png

I want it to be possible to transfer data from the swagger , I

add

@swagger_auto_schema(method='post', manual_parameters=[
    openapi.Parameter('user_login', type=openapi.TYPE_STRING, description='user_login', in_=openapi.IN_FORM),
    openapi.Parameter('password', type=openapi.TYPE_STRING, description='password', in_=openapi.IN_FORM)
    ])


gives an error message

File "C:\WORK\210502-urrobot\urenv\lib\site-packages\drf_yasg\inspectors\view.py", line 166, in add_manual_parameters
    raise SwaggerGenerationError("cannot add form parameters when the request has a request body; "
drf_yasg.errors.SwaggerGenerationError: cannot add form parameters when the request has a request body; did you forget to set an appropriate parser class on the view?


adding

@parser_classes((FormParser,))

doesn't help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Antonio Solo, 2021-05-03
@solotony

in general, quite by accident, I discovered that the order of decorators
is important, and in this form everything worked :)

@swagger_auto_schema
@api_view(('POST',))
@parser_classes((FormParser,))
def user_login(request):
   ...

ps 3 hours of time and a lot of mats.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question