I want to pass data from my view to my template in an efficient way. Specifically I need an array with a dict inside of it like so:
arraydict = [{key:value}, {key:value} ...]
Right now this is what I do:
class VisualizationView(ListView):
template_name = 'visualize.html'
model = UrlTime
def get_context_data(self, **kwargs):
context = super(VisualizationView, self).get_context_data(**kwargs)
#Extracts the months and counts them
context['months'] = (UrlTime.objects.annotate(month=ExtractMonth('timestamp')).values('month').annotate(c=Count('id')))
this counts my months. Printing our my {{ month }} in my django template gives me back this queryset:
<QuerySet [{'month': 5, 'c': 313}, {'month': 6, 'c': 1961}]>
So far so good. My idea is to visualize my data with d3.js for which I need well-strucutred data. So I would like to transform this queryset into something like:
data = [{5:313}, {6:1961}]
. Basically creating a dict of the values of the queryset and then put it into an array.
(Even better would be data = [{May:313}, {June:1961}]
)
I tried the .values() method which breaks my count method. Also I tried to put it into a list like so:
list(UrlTime.objects.annotate(month=ExtractMonth('timestamp')).values('month').annotate(c=Count('id')))
(this works but gives me back a list with dict of keys and arrays like so: [{'month': 5, 'c': 313}, {'month': 6, 'c': 1962}]
I also did some looping in my template, yet my idea is to put most of the logic into my view, so I'd prefer the code outside the template.
Is there an easy way to do this or am I on the wrong track? Any help is very much appreciated. Thanks in advance.