0👍
Based on your input, I guess first request is calling home
view and loads initial set data – which is then displayed on html page. On the other hand, then you are using ChartData
API view. If this scenario is right – then:
The problem in ChartData
is that, this part time, set1 , set2 , set3 = get_sets()
is executed one time when django is initialized (as described). What you need is to get sets each time when request is sent. And the code fragment is executing upon request is in get
method.
Right solution would look like this:
class ChartData(APIView):
authentication_classes = []
permission_classes = []
sets = Sets.objects.all()
def get(self, request, format=None):
time, set1 , set2 , set3 = get_sets()
data = {
"time": time,
"set1": set1,
"set2": set2,
"set3": set3
}
return Response(data)
There is no need for extra class variables (self.set1
…). And the get_sets
function is called each time request reaches the get
method.
Update (based on comment)
To avoid loading whole data each time request is handled by API View – I would keep track of last record received in frontend and based on last record id (there may be another field) I would fetch records created after the id you’re tracking in frontend.