- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我注意到当我使用 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/
我目前正在尝试定义函数,但遇到了这个错误。我只是想做一个简单的函数,用户输入 2 个数字,然后将它们相乘。也请尽可能简单地解释我做错了什么。 (我是菜鸟) def userinput(): w
使用IPtools python 包我试图查看 IP 地址是否在特定范围内。这是我的代码: for line in g: org= line.split("|")[0] ranges
输入 [['1','2','3'],['a','b','c'],['6','7','8'],['e','f','g']] 输出应该是: 1, 2, 3a, b, c6, 7, 8e, f, g Cod
我目前正在使用 lambda 使 tkinter 按钮依次执行两件事: def classManip(): cManip = tk.Toplevel() cManip.title
我正在学习Python,作为练习,我编写了一些代码来查找用户定义函数的导数。代码如下。 def fx(value, function): x = value return eval(f
使用 Django。我有以下模型: class Postagem(models.Model): id = models.AutoField(primary_key=True, editable=Fal
我正在尝试为给定的数据集选择重要的特征(或者至少了解哪些特征解释更多的变异性)。为此,我使用 ExtraTreesClassifier 和 GradientBoostingRegressor - 然后
刚刚获得了SDXL模型的访问权限,希望为即将发布的版本进行测试...不幸的是,我们当前用于我们服务的代码似乎不能与稳定ai/稳定-扩散-xl-base-0.9一起工作,我不完全确定SDXL有什么不同,
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我想遍历可迭代列表,但要求某些元素的类型可以是 None。 这看起来像这样: none_list = [None, [0, 1]] for x, y in none_list: print("
我得到object is not subscriptable在非空查询结果上。当我打印时 c.fetchone()它打印了正确的结果,但是当我检查类型时它显示 import sqlite3 conn
我在第 15 行收到此错误,但我不明白为什么。有任何想法吗?看来属性已经明确定义了,所以我完全不知所措。任何帮助将非常感激。AttributeError:“NoneType”对象没有属性“Sheets
我尝试对 Chrome WebDriver 进行子类化以包含一些初始化和清理代码,但随后 Python 提示创建的对象设置为 None: import glob import selenium imp
这个问题已经有答案了: Why do I get AttributeError: 'NoneType' object has no attribute 'something'? (10 个回答) 已关
这个问题已经有答案了: Why does the print function return None? (1 个回答) 已关闭 6 年前。 我对 Python 还很陌生。我正在制作一个生成器,可以为
我正在尝试比较两个表( table_a 和 table_b )并减去 table_a 的最后一列从table_b的最后一列开始。但是,table_a 包含一个额外的行,导致我得到 NoneType错误
当“文件名”是一个存在的文件时,这段代码运行良好……但是当它不存在时……我不断收到同样的错误:TypeError: 'NoneType' 对象不可迭代 (Errno 2) 尽管我从不迭代任何东西,除非
我在下面的代码中收到“NoneType”对象不可迭代的 TypeError。下面的代码用于使用 pyautogui 滚动 digits 文件夹中的 10 张图像(命名为 0 到 9,以图像中的 # 命
我有一段代码表现得很奇怪。 一开始,我导入了一个模块,它是 C 库的 python 绑定(bind)。 try: import pyccn except: print "ERROR:
我是一名优秀的程序员,十分优秀!