Update model object with new dynamicaly created field?
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.
精彩评论