1👍
✅
Rather than performing a query per day, you can retrieve all objects ordered by date and then use itertools.groupby()
to split them into dates.
def data_points(self):
trades = self.get_queryset()
data_x = []
data_y = []
for date, subset in itertools.groupby(trades, lambda t: t.date):
average_price = average(subset) # average() needs to be implemented
if average_price > 0:
data_x.append(date.strftime('%Y-%m-%d'))
data_y.append(average_price)
return data_x, data_y
This approach trades web server CPU for DB CPU/IO, this may or may not be the best approach depending on your infrastructure
Source:stackexchange.com