gpt4 book ai didi

python - Django - 属性错误 : 'NoneType' object has no attribute 'method'

转载 作者:行者123 更新时间:2023-12-05 07:39:36 27 4
gpt4 key购买 nike

我注意到当我使用 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)

没有 self 的回溯:

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

self 回溯:

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'

最佳答案

删除 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)

关于python - Django - 属性错误 : 'NoneType' object has no attribute 'method' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47012130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com