我正在尝试使用DRF构建Web应用程序。我正在尝试传递客户ID并显示他已完成的最大交易。
这是我的views.py
class max_val(APIView):
def post(self, request, *args, **kwargs):
cid = json.loads(request.body).get('cid')
queryset = model_name.objects.filter(customer_id=cid).aggregate(Max('transaction'))
serialize = serializer_name(queryset, many=True)
return Response(serialize.data, status=status.HTTP_200_OK)
当我运行它时,我得到-
HTTP 200 OK
Allow: POST, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"customer_id": null,
"transaction": null
}
]
这是我的序列化器-
class serialser_name(serializers.ModelSerializer):
class Meta:
model = model_name
fields = ('customer_id', 'transaction')
它没有返回最大值。我该怎么办?
答案 0 :(得分:0)
聚集不返回对象,它仅返回field_name__max及其值的字典。尝试此操作:
class max_val(APIView):
def post(self, request, *args, **kwargs):
cid = json.loads(request.body).get('cid')
max_transaction = model_name.objects.filter(customer_id=cid).aggregate(Max('transaction'))
return Response(max_transaction, status=status.HTTP_200_OK)
您可以找到有关聚合here的更多信息。