2009-12-01 3 views
0



queryset = Status.objects.all()[:10]
Status 모델 commentAmountcommentAmount 필드를 가지고 있지 않아서 모든 개체에 추가 할 수 있습니다.

동적으로 생성 된 새로운 필드로 모델 객체를 업데이트 하시겠습니까?

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 

JSON 파일을 반환 할 때 commentAmount 필드가 없습니다. 내 실수는 어디 갔지?

답변

2

이유는 commentAmount이 나타나지 않기 때문입니다. Django가 직렬화를 수행 할 때 모델에서 선언 된 필드와 해당 필드 만 루프하기 때문입니다.

템플릿에서 쿼리 세트를 반복하고 json을 수동으로 만들거나 simplejson과 같은 다른 serialization 도구를 사용하는 것이 좋습니다.

관련 문제