【发布时间】:2017-02-08 13:49:11
【问题描述】:
我遇到了一个错误, /ResultJSON/v1/results/ 处的 AttributeError 'list' 对象没有属性 'id' 。
在 ResultJSON(child app) 的 views.py 中,我写了
import json
from collections import OrderedDict
from django.http import HttpResponse
def render_json_response(request, data, status=None):
json_str = json.dumps(data, ensure_ascii=False, indent=2)
callback = request.GET.get('callback')
if not callback:
callback = request.POST.get('callback')
if callback:
json_str = "%s(%s)" % (callback, json_str)
response = HttpResponse(json_str, content_type='application/javascript; charset=UTF-8', status=status)
else:
response = HttpResponse(json_str, content_type='application/json; charset=UTF-8', status=status)
return response
def UserResult(request):
results = []
results = OrderedDict([
('id',results.id),
('name', results.name),
])
results.append(results)
data = OrderedDict([ ('results', results) ])
return render_json_response(request, data)
我真的不明白为什么会发生这个错误,因为我的数据库(sqlite)有 id 列。
我想让系统从数据库中获取数据(id&name 是列的名称,这些数据在我的数据库中)并将这些数据编码为 JSON。
那么,我该如何解决这个问题?我认为也许 models.py 是错误的......(因为子应用的 models.py 没有代码。)
【问题讨论】:
-
results = []后跟foo = ... results.id ...看起来很可疑!