我正在为我的 Django 网络应用程序创建移动应用程序。我是这样设置的
模型.py
class Comment(models.Model):
CommentDescription = models.CharField(max_length=120)
Owner = models.ForeignKey(User,null=True)
PostToCommentOn = models.ForeignKey(Post, null=True)
View .py
def sendJsonDataBackToMobileClient(request,postId):
comments = Comment.objects.filter(PostToCommentOn = postId)
jsonString = #create an organized json string from comment models
return HttpResponse(jsonString)
我不确定如何组织我的评论模型的 json 以快速对我的移动应用程序客户端做出轻量级响应
最佳答案
查看JsonResponse :
>>> from django.http import JsonResponse
>>> response = JsonResponse({'foo': 'bar'})
>>> response.content
b'{"foo": "bar"}'
关于python - 将 JSON 数据发送到客户端 django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382514/