有一个对象列表,打印方式如下:
[<Intermediate_table: Intermediate_table object>, <Intermediate_table: Intermediate_table object>, <Intermediate_table: Intermediate_table object>, <Intermediate_table: Intermediate_table object>]
我想提取我使用 json.loads(queryset)的单个对象的email属性,它显示错误:期望值:第1行第1列(char 0 )
#I also tried without json.loads:
for i in queryset:#the list is in queryset variable
print(i.subject)#this gives an error
该函数(在admins.py文件中,它会在此模型的管理页面上调用特定操作):
def authenticate_(modeladmin, request, queryset):
#q = queryset.email;
#req = json.loads(str(queryset )
#q = req['email']
for i in queryset:
print(json.loads(str(i)))
print('asdasdadasd-->'+str(queryset))
错误追踪:
Traceback:
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/contrib/admin/options.py" in wrapper
465. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
25. return func(self, *args2, **kwargs2)
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/contrib/admin/options.py" in changelist_view
1364. response = self.response_action(request, queryset=cl.get_queryset(request))
File "/home/saurabh/Music/Project_ques10/test/lib/python3.4/site-packages/django/contrib/admin/options.py" in response_action
1117. response = func(self, request, queryset)
File "/home/saurabh/Music/Project_ques10/ques10-funda/biostar/apps/quespaper/admin.py" in authenticate_
83. print(json.loads(str(i)))
File "/usr/lib/python3.4/json/__init__.py" in loads
318. return _default_decoder.decode(s)
File "/usr/lib/python3.4/json/decoder.py" in decode
343. obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.4/json/decoder.py" in raw_decode
361. raise ValueError(errmsg("Expecting value", s, err.value)) from None
Exception Type: ValueError at /admin/quespaper/intermediate_table/
Exception Value: Expecting value: line 1 column 1 (char 0)
请建议解析每个对象的正确方法。
感谢。
答案 0 :(得分:0)
问题是您尝试解码的对象首先不是json可编码的。 JSON可接受的类型是string,number,boolean,array和dict。因此,如果您将此对象转换为字符串并尝试对其进行解码,那么python将无法将其解码为正确的对象,因此会引发错误。
如果您真的想使用JSON格式,只有在首先对其进行编码后才能在字符串表示中使用它:
Measurement
您可能还需要为模型定义_str __()方法。
没有json.loads()的代码失败,因为您的Intermediate_table对象没有属性电子邮件。