【发布时间】:2014-12-22 21:42:47
【问题描述】:
我正在尝试在 django 中进行调查应用。我的模型如下:
class mymodel(models.Model):
resptype = models.ForeignKey(Response)
ques = models.ForeignKey(Question)
response = models.CharField(max_length=5, blank=True)
这里我使用 rest framework 将数据发送到我的前端。现在我的api 定义如下:
class mymodelList(APIView):
def get(self, request, format=None):
surveydata = mymodel.objects.all()
serialized_surveydata = mymodelSerializer(surveydata, many=True)
return Response(serialized_surveydata.data)
在我的应用程序中,我有一组标准的 16 个问题,其中包含多项选择响应,并且选择保存在模型的响应列中。
现在我想要实现的是计算每个问题的响应数。 IE。对于问题 1,一个人回答 1 或 2 等的计数是多少。
我还想知道如何通过来自其余框架的另一个 json 字段发送计算的计数,因为我没有为此数据单独定义任何模型。
编辑:
这个命令完成了我的查询,但我仍然无法弄清楚如何将它作为序列化对象发送到前端。
x = mymodel.objects.values('ques','response').order_by().annotate(number_of_responses=Count('response'))
【问题讨论】:
标签: django django-models django-rest-framework