I've a table Orders which contains ID, store_name, order_date, ..... I want to create an endpoint which returns me the JSON which consists of Count of Orders from all the Stores. Something as follows:
[{store_name: 'Target', count: 10}, {store_name: 'Walmart', count: 20}, {store_name: 'Costco', count: 5}]
The query I wrote is:
queryset = Stores.objects.all().values('store_name').annotate(total=Count('store_name'))
When I print queryset, I'm getting what I need as mentioned above. But when I serialize the data, I get the following:
[{store_name: 'Target'}, {store_name: 'Walmart'}, {store_name: 'Costco'}]
Not sure what am I doing wrong.. I've included my code. (I'm not including import statements)
serializer.py
class StoresSerializer(ModelSerializer):
class Meta:
model = Stores
exclude = ['order_date',]
views.py
class StoresViewSet(ModelViewSet):
queryset = Stores.objects.all().values('store_name').annotate(total=Count('store_name'))
serializer_class = StoresSerializer
What am I missing?