In order to plot a chart with Highcharts I have to structure a queryset in a list of dictionaries that have to look like the following:
[{
name: 'Europe',
data: [{
name: 'Germany',
value: 767.1
}, {
name: "Cyprus",
value: 7.2
}]
}, {
name: 'Africa',
data: [{
name: "Senegal",
value: 8.2
}, {
name: "Ghana",
value: 14.1
}]
}]
The following code is the way I tried. The data is structured as requested and the graph is plotted good, but the final list (listsocios) is missing some values and repeating another ones. I guess it is memory issue. Whats your thoughs? Whats would be the more effecient way to struct?
class GraphSociosProvinciasView(TemplateView):
template_name = 'socios/socios_prov_graph.html'
def get_context_data(self,*args, **kwargs):
context = super(GraphSociosProvinciasView, self).get_context_data(*args,**kwargs)
listsocios=[]
listsocprov=[]
#Por cada provincia guardo los socios
for n in range(1,25):
sociosp=Socios.objects.filter(provincia=n)
TotalSocios=sociosp.count()
listsocprov.clear()
if TotalSocios!=0:
for socp in sociosp:
listsocprov.append({'name': socp.apellido,'value': socp.numero_socio},)
nombre_provincia=socp.get_provincia_display()
listsocios.append({'name':nombre_provincia,'data':listsocprov})
time.sleep(1)
print(listsocios)
context={
'sociosprov': listsocios,
}
return context