I'm trying to write a method which would sum the view_count values from my "pages" in models, but for some reason the result that i get on the template is written in a weird fashion Overall views: {'view_count__sum': 9}
The model looks like this:
class Page(models.Model):
name = models.CharField(max_length=20, primary_key=True)
content = models.TextField(blank=True)
view_count = models.IntegerField (default= 0)
edits = models.IntegerField(default=0)
This is a snippet of my view in which i tried to define a context method for my templates:
def context_count(request):
page_count = Page.objects.count()
tracker = Page.objects.all().aggregate(Sum(F('view_count'), output_field=IntegerField()))
def __str__(self):
return self.tracker
return { 'page_quantity':page_count, 'page_viewcount':tracker}
Just to make sure i fill you in fully, this is the line of code which my template uses to display the variable
<p>
Overall views: {{page_viewcount}}
</p>
I realize that I have skipped over something basic, but I appreciate any input that i get on this.
From the Django docs:
aggregate() is a terminal clause for a QuerySet that, when invoked, returns a dictionary of name-value pairs.
tracker
is a dictionary. So you need to return this:
return {'page_quantity': page_count, 'page_viewcount': tracker['view_count__sum']}