我正在为我的django web应用程序创建移动应用程序。我有这样的设置
models.py
class Comment(models.Model):
CommentDescription = models.CharField(max_length=120)
Owner = models.ForeignKey(User,null=True)
PostToCommentOn = models.ForeignKey(Post, null=True)
def sendJsonDataBackToMobileClient(request,postId):
comments = Comment.objects.filter(PostToCommentOn = postId)
jsonString = #create an organized json string from comment models
return HttpResponse(jsonString)
我不确定如何组织我的评论模型的json来快速对我的移动应用程序客户端做出轻量级的响应
答案 0 :(得分:3)
查看JsonResponse:
>>> from django.http import JsonResponse
>>> response = JsonResponse({'foo': 'bar'})
>>> response.content
b'{"foo": "bar"}'