I have this model which is related to a Node model,
class Views(models.Model):
related_tree = models.ForeignKey(Node, on_delete=models.CASCADE, blank=True, null=True, related_name='related_views')
views_count = models.PositiveIntegerField(null=True, blank=True, default=0)
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return self.related_tree.name
In the template when i query a list of the Node's objects i want to aggregate the value of the 'views_count' of each object.
I have tried this in my template
{% for tree in tree_result|slice:":14" %}
{% for view in tree.related_views.all %}
{{ view.views_count }}
{% endfor %}
{% endfor %}
i have all the related count_view of the current tree but what i want to do is to aggregate the value of the related field and show it within the for loop of tree_result.
Any suggestions please ?
EDIT :
def ResultSearch(request, search):
formatted_search = search.split('+$')
filter = Q()
cases = {}
get_result_list = [x for x in search.split('+$') if x != '']
for i, keyword in enumerate(get_result_list):
filter |= Q(name__icontains=keyword)
filter |= Q(Tags__icontains=keyword)
cases['keyword_match_{}'.format(i)] = Case(
When(name__icontains=keyword, then=1),
default=Value(0),
output_field=models.IntegerField(),
)
result = Node.objects.filter(
filter,
tree_type='root',
published=True,
tunisia_office=True
).annotate(
**cases
).annotate(
total_views=Sum('related_views__views_count'),
num_bookmarks=Count('bookmarked_by'),
keywords_matched=reduce(add, (F(name) for name in cases))
).order_by('-keywords_matched', '-num_bookmarks')
context = {
'searched_keyword': formatted_search,
'result_list': result,
}
You should annotate your queryset in the view, like:
from django.db.models import Sum
Node.objects.annotate(
total_views=Sum('related_views__views_count')
)
The Node
s that arise from this queryset will have an extra attribute .total_views
which is the sum of the views_count
of the related Views
.
In your template you can then render this like:
{% for tree in tree_result|slice:":14" %}
{{ tree.total_views }}
{% endfor %}
Note: please do not aggregate in the template. Templates are not designed to implement business logic. A template should only contain "render logic". By using annotations, you will let the database do the aggregations. Databases are usually optimized to perform such tasks.