I have a Django queryset being created for a graph like the following:
obj = Allotment.objects
.all()
.values('dispatch_date')
.annotate(transaction_no=Count('transaction_no'))
.values('dispatch_date', 'transaction_no')
Now I am trying to pass all these values dynamically so I tried this :
data = request.data.copy()
x_model = data['x_model']
y_model = data['y_model']
obj = Allotment.objects
.all()
.values('{}'.format(x_model))
.annotate(transaction_no=Count('{}'.format(y_model)))
.values('{}'.format(x_model), '{}'.format(y_model))
where I just replaced the string values and it gives me the following error:
The annotation 'transaction_no' conflicts with a field on the model.
How do I dynamically change the field in annotate function? and apply aggregators such as
Sum
, Count
, etc given that the aggregators are passed through the API as well