I have following model:
class Claim:
amount = models.PositiveIntegerField()
I am trying to create API that dynamically sends response in such way that amount ranges are dynamic.
For example my minimum Claim amount is 100 and max is 1000 I wanted to show JSON in this way:
{
"100-150": 2,
"150-250": 3,
"250-400": 1,
"400-500": 5,
"above_500": 12
}
I tried doing this way assuming my data range is between 1 and 2000 but this becomes of no use if my minimum amount lies in between 10000 and 100000.
d = Claim.objects.aggregate(upto_500=Count('pk', filter=Q(amount__lte=500)),
above_500__below_1000=Count('pk', filter=Q(amount__range=[501, 999])),
above_1000__below_2000=Count('pk', filter=Q(amount__range=[1000, 2000])),
above_2000=Count('pk', filter=Q(amount__gte=2000))
)
Any idea how we can make dynamic way of getting amount ranges and throwing it to frontend?