Answer the question
In order to leave comments, you need to log in
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_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)
])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?Answer the question
In order to leave comments, you need to log in
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):
...Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question