Django Rest Framework“列表”对象没有属性值

我有下面的代码和错误堆栈跟踪。


我正在尝试访问localhost:8000/fundamentals/,但收到错误消息'list' object has no attribute 'values'


错误


web_1  | Traceback (most recent call last):

web_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner

web_1  |     response = get_response(request)

web_1  |   File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 202, in _get_response

web_1  |     response = response.render()

web_1  |   File "/usr/local/lib/python3.7/site-packages/django/template/response.py", line 105, in render

web_1  |     self.content = self.rendered_content

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/response.py", line 70, in rendered_content

web_1  |     ret = renderer.render(self.data, accepted_media_type, context)

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/renderers.py", line 724, in render

web_1  |     context = self.get_context(data, accepted_media_type, renderer_context)

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/renderers.py", line 655, in get_context

web_1  |     raw_data_post_form = self.get_raw_data_form(data, view, 'POST', request)

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/renderers.py", line 563, in get_raw_data_form

web_1  |     data = serializer.data.copy()

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 562, in data

web_1  |     ret = super().data

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 264, in data

web_1  |     self._data = self.get_initial()

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 412, in get_initial

web_1  |     for field in self.fields.values()

web_1  |   File "/usr/local/lib/python3.7/site-packages/rest_framework/serializers.py", line 413, in <listcomp>

胡子哥哥
浏览 94回答 1
1回答

动漫人物

这里的问题是与BalanceSheetSerializer.&nbsp;必须fields在类中定义,Meta而不是将其定义为类变量。class&nbsp;BalanceSheetSerializer(serializers.ModelSerializer): &nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;Meta: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fields&nbsp;=&nbsp;[your_fields]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python