WSGIRequest object has no 'data' attributes

I am having trouble sending to my API and I canโ€™t understand what this is about. If that matters, I use Django REST and enable tracing.

if (repeat == false) { post_data = {'User': usernameInput} $.ajax({ type: 'POST', url: '/0/addUser', data: post_data, async: true }) } class AddUser(APIView): def post(self, request, format = None): serializer = UserSerializer(data=request.data) if serializer.isvalid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) Traceback: File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view 57. return view_func(*args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in view 69. return self.dispatch(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch 403. response = self.handle_exception(exc) File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch 400. response = handler(request, *args, **kwargs) File "/Users/rae/Desktop/112/djangotemplate/notes/views.py" in post 23. serializer = UserSerializer(data=request.data) File "/Library/Python/2.7/site-packages/rest_framework/request.py" in __getattr__ 436. return getattr(self._request, attr) 
+5
source share
1 answer

The Django REST Framework has its own Request object, which wraps the HttpRequest object passed to Django and adds some additional features (such as custom rendering and another level of authentication). If a Request object, which does not exist, has any properties, it automatically proxies it to the base HttpRequest , so usually you donโ€™t notice the difference.

In DRF 2.x, the Request property has the DATA and FILES properties, which store the transferred data, as well as any detected files. They were merged into DRF 3.0 and replaced by a single DATA property. When DRF 3.0 was released, all documentation now reflects the new Request.data property.

You seem to be using Django REST Framework 2.x, but you are trying to access the new property introduced in DRF 3.0. Since it does not exist in the Request object, it is proxied to the HttpRequest object, where it is also not found.

+8
source

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


All Articles