属性错误:“ExampleView”对象没有属性“数据”; Django 休息框架
我必须在基于类的视图中编写一个自定义函数,如下所示。
class ExampleView(ListCreateAPIView,
DestroyAPIView,
RetrieveUpdateAPIView):
queryset = Example.objects.all()
serializer_class = ExampleSerializer
def dispatch(self, request, *args, **kwargs):
if request.method == 'PUT' and kwargs.get('slug'):
return self.custom_function(request,*args,**kwargs)
return super().dispatch(request, *args, **kwargs)
def custom_function(self, request, *args, **kwargs):
instance = self.get_object()
print(instance)
slug = kwargs.get("slug")
print(slug)
print(request.data)
在这里我可以看到,从调度中,调用了自定义函数,并且我可以看到实例和 slug 的打印,但是当我打印(request.data)时,它显示以下错误:
AttributeError: 'ExampleView' object has no attribute 'data'
我需要 request.data 来执行数据中的一些逻辑。
I have to write a custom function inside class based view which is like below.
class ExampleView(ListCreateAPIView,
DestroyAPIView,
RetrieveUpdateAPIView):
queryset = Example.objects.all()
serializer_class = ExampleSerializer
def dispatch(self, request, *args, **kwargs):
if request.method == 'PUT' and kwargs.get('slug'):
return self.custom_function(request,*args,**kwargs)
return super().dispatch(request, *args, **kwargs)
def custom_function(self, request, *args, **kwargs):
instance = self.get_object()
print(instance)
slug = kwargs.get("slug")
print(slug)
print(request.data)
Here I can see that from dispatch, the custom function is called and I can see the print of instance and slug but when I print(request.data) it says the error below:
AttributeError: 'ExampleView' object has no attribute 'data'
I need request.data to perform some logic in the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论