2009-12-01 105 views
0

我有查询集:
queryset = Status.objects.all()[:10]
型号Status没有得到现场commentAmount所以我把它添加到每一个对象在查询集:
使用新动态创建的字段更新模型对象?

for s in queryset: 
    s.commentAmount = s.getCommentAmount() 

一切都很好,print s.commentAmount显示效果不错,但后:

response = HttpResponse() 
response['Content-Type'] = "text/javascript" 
response.write(serializers.serialize("json", queryset)) 

return response 

我没有字段commentAmount在返回JSON文件。我的错误在哪里?

回答

2

commentAmount没有显示的原因是因为当Django进行序列化时,它循环遍历模型中声明的字段以及仅这些字段。

考虑循环遍历模板中的查询集并手动创建json或使用另一个序列化工具(如simplejson)。

相关问题