开发者

Update model object with new dynamicaly created field?

开发者 https://www.devze.com 2022-12-13 11:46 出处:网络
I have Queryset: queryset = Status.objects.all()[:10] Model Status hasn\'t got field commentAmount so I would add it to every object in Queryset:

I have Queryset:

queryset = Status.objects.all()[:10]

Model Status hasn't got field commentAmount so I would add it to every object in Queryset:

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

All is fine, print s.commentAmount shows good results, but after:

response = HttpResponse()
response['Content-Type'] = "text/javascript"
response.write(serializers.ser开发者_JAVA百科ialize("json", queryset))

return response

I have not field commentAmount in returning JSON file. Where is my mistake?


The reason commentAmount is not showing up is because when Django does the serialization, it loops through the fields declared on the model and only those fields.

Consider looping through your queryset in a template and creating the json manually or using another serialization tool such as simplejson.

0

精彩评论

暂无评论...
验证码 换一张
取 消