getting the sum of prices for each user

I have a table that has fields of user and prices. I want to display total price of each user by itself just like the photo shows 2 users ahmad and taleen so i need to show ahmad records then the total is 5 for ahmad then taleen records then the total is 5 for taleen.


here is the code:

views.py

def prayer_reports(request):
	prices = []
	records = Prayer.objects.all().order_by('user')

	for i in records:
		prices.append(i.price)

	total_price = sum(prices)

	return render(request, 'prayers/prayer_reports.html',{'records':records,'total_price':total_price})

models.py

class Prayer(models.Model):
	user = models.CharField(max_length=50, null=True, default="User")
	name = models.CharField(max_length=50, null=True,default="Prayer")
	time_entered= models.TimeField(default=datetime.now().strftime("%H:%M"))
	on_time = models.BooleanField(default=True)
	date=models.DateTimeField(default=timezone.now)
	price = models.FloatField(default=0.0)

	def __str__(self):
		return self.name

prayer_reports.html:

{% extends 'docs/main.html' %}
{% block content %}

{% if user.is_superuser %}
	{% for i in records %}
		<h3>Name: {{i.user}}</h3>
		<h3>Prayer: {{i.name}}</h3>
		<h3>Date: {{i.date}}</h3>
		<h3>Price: {{i.price}}</h3>
		<br/>
	{% endfor %}
{% endif %}

{% endblock content %}

how can i have this working?

Hey there!
Take a look into annotate/aggregate on the docs.

In your case, you probably will want to use annotate.