TruncMonth on a queryset


I have a class based view which returns some headline objects from a model to a queryset. I’m wanting to use TruncMonth to return the months where these occur so I can show a timeseries graph.

class FilterHeadlines(ArchiveIndexView):
    model = Headline
	def get_queryset(self, *args, **kwargs):
        queryset = super(FilterHeadlines, self).get_queryset()
		queryset = Headline.objects.all()
        return queryset

Is it possible to return not only the queryset, but also the time series data in the same view? I was hoping I could include the below, but I get “‘QuerySet’ object does not support item assignment”

queryset['trends'] = queryset.annotate(month=TruncMonth('published_date')).values('month').annotate(headlines=Count('id')).order_by('month')

Anyone able to point me in the right direction?


get_queryset isn’t the only way you make data available in your view to be rendered.

Additional data is made available to the context using get_context_data.

Also, when trying to understand how CBVs work, I find it very useful to reference the Classy Class-Based Views site and the CBV diagrams page.

Thanks @KenWhitesell. I changed my view over to get_context_data and was able to send over a bunch of other data in addition to just the queryset. The side effect was that I lost my pagination which is included in the ArchiveIndexView class view - ended up doing a bit of a copy paste to re-implement this manually from that link you sent over (thanks for that too!). It’s weird I lost pagination as some of the functionality of the view seemed to remain, ie date_list which I was able to access with self.date_list. Hoping that it’s not having to do this pagination stuff twice now, what’s built into the view, and then what I have added shrug