I'm overriding the def get_queryset(self)
to aggregate averages, sums, etc, of a model. My challenge is that I want to return these values but it is not a queryset so I get the following error: django.template.response.ContentNotRenderedError: The response content must be rendered before it can be iterated over.
. My question is how can I return these values since they are not a queryset.
Here is my code:
class OrderMetrics(generics.ListCreateAPIView ):
queryset = Order.objects.all()
serializer_class = OrderSerializer
def get_queryset(self):
...
context = [
{
'data': round(current_total),
'percent': abs(total_delta_percentage),
},
{
'data': current_count,
'percent': abs(avergae_delta_percentage),
},
]
print(context)
return Response(context, status=status.HTTP_200_OK)
# print(context) returns the following:
# [{'data': 138378, 'percent': 132}, {'data': 11, 'percent': 16}]
I have seen several other similar questions but they don't address how to do this: ContentNotRenderedError: The response content must be rendered before it can be iterated over. Django REST and ContentNotRenderedError : The response content must be rendered before it can be iterated over