在django中引入xadmin后出现数十个报错信息,通过这篇博客:django安装xadmin及问题处理即可解决绝大部分问题,但本文题目中标注的问题无法解决。
1 报错分析
报错信息为:
······
File "D:\PythonWork\CourseWeb\xadmin\views\edit.py", line 220, in get_form_layout
rendered_fields = [i[1] for i in layout.get_field_names()]
File "D:\PythonWork\CourseWeb\xadmin\views\edit.py", line 220, in <listcomp>
rendered_fields = [i[1] for i in layout.get_field_names()]
TypeError: 'Pointer' object is not subscriptable
[11/Jun/2023 21:53:56] "GET /xadmin/users/userprofile/1/update/ HTTP/1.1" 500 186820
点击报错信息,转到报错位置,内容如下:
layout = Layout(Container(*fs))
if layout.get_field_names():
rendered_fields = [i[1] for i in layout.get_field_names()]
else:
rendered_fields = []
其中i[1]
报错,原因是i
为pointer
对象,不能使用下标[1]
控制。
我们输出pointer对象:
[Pointer(positions=[0, 0, 0, 0], name='username'),
Pointer(positions=[0, 0, 0, 1], name='password'),
Pointer(positions=[0, 0, 1, 0, 0, 0], name='first_name'),
Pointer(positions=[0, 0, 1, 0, 1, 0], name='last_name'),
Pointer(positions=[0, 0, 1, 1], name='email'),
Pointer(positions=[0, 0, 2, 0], name='groups'),
......]
即可发现,i[1]
实际上想表达的是i.name
,所以我们将i[1]
改为i.name
即可。
2 解决方案
将
layout = Layout(Container(*fs))
if layout.get_field_names():
rendered_fields = [i[1] for i in layout.get_field_names()]
else:
rendered_fields = []
改为:
layout = Layout(Container(*fs))
if layout.get_field_names():
rendered_fields = [i.name for i in layout.get_field_names()]
else:
rendered_fields = []
至此,问题解决。