我注意到,当我使用DRF文档时,我收到错误AttributeError: 'NoneType' object has no attribute 'method'
如果self.request.method == 'POST'
这与此行相关联,那么我有什么想法可以解决它?
views.py:
@permission_classes([UserPermission])
class UserObject(GenericAPIView):
def get_serializer_class(self):
if self.request.method == 'POST':
return ObjectPostSerializer
return ObjectSerializer
def post(self, request, user_id):
serializer = ObjectSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user_id=user_id)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, user_id):
try:
object = Object.objects.filter(user=user_id)
except Object.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = ObjectSerializer(object, many=True)
return Response(serializer.data)
def put(self, request, user_id):
try:
object = Object.objects.get(user=user_id)
except Object.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
serializer = ObjectSerializer(object, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, user_id):
try:
object = Object.objects.filter(user=user_id)
except Object.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
object.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
没有自我的追踪:
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
django_1 | response = get_response(request)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
django_1 | response = self.process_exception_by_middleware(e, request)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
django_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
django_1 | return view_func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
django_1 | return self.dispatch(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
django_1 | response = self.handle_exception(exc)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
django_1 | self.raise_uncaught_exception(exc)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
django_1 | response = handler(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/views.py", line 31, in get
django_1 | schema = self.schema_generator.get_schema(request, self.public)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 279, in get_schema
django_1 | links = self.get_links(None if public else request)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 317, in get_links
django_1 | link = view.schema.get_link(path, method, base_url=self.url)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 167, in get_link
django_1 | fields += self.get_serializer_fields(path, method)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 290, in get_serializer_fields
django_1 | serializer = view.get_serializer()
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/generics.py", line 110, in get_serializer
django_1 | serializer_class = self.get_serializer_class()
django_1 | File "/code/backend/views.py", line 173, in get_serializer_class
django_1 | if request.method == 'POST':
django_1 | NameError: name 'request' is not defined
自我跟踪:
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
django_1 | response = get_response(request)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
django_1 | response = self.process_exception_by_middleware(e, request)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
django_1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
django_1 | return view_func(*args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
django_1 | return self.dispatch(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
django_1 | response = self.handle_exception(exc)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
django_1 | self.raise_uncaught_exception(exc)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
django_1 | response = handler(request, *args, **kwargs)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/views.py", line 31, in get
django_1 | schema = self.schema_generator.get_schema(request, self.public)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 279, in get_schema
django_1 | links = self.get_links(None if public else request)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/generators.py", line 317, in get_links
django_1 | link = view.schema.get_link(path, method, base_url=self.url)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 167, in get_link
django_1 | fields += self.get_serializer_fields(path, method)
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/schemas/inspectors.py", line 290, in get_serializer_fields
django_1 | serializer = view.get_serializer()
django_1 | File "/usr/local/lib/python3.6/site-packages/rest_framework/generics.py", line 110, in get_serializer
django_1 | serializer_class = self.get_serializer_class()
django_1 | File "/code/backend/views.py", line 173, in get_serializer_class
django_1 | if self.request.method == 'POST':
django_1 | AttributeError: 'NoneType' object has no attribute 'method'
答案 0 :(得分:0)
您正在使用带有基于类的视图的函数装饰器。这将导致未定义的行为,因为类实例将无法正确初始化。
@permission_classes([UserPermission])
class UserObject(GenericAPIView):
...
而是使用类属性。
class UserObject(GenericAPIView):
permission_classes = [UserPermission]
文档:http://www.django-rest-framework.org/api-guide/permissions/#setting-the-permission-policy
答案 1 :(得分:0)
删除get_serializer_class
方法,你就可以了。
<强>修饰强>:
class UserObject(GenericAPIView):
serializer_class = ObjectSerializer
def post(self, request, user_id):
# this is here you change the serializer.
serializer = ObjectPostSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user_id=user_id)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)