# getting the sum of prices for each user

**URL:** <https://forum.djangoproject.com/t/getting-the-sum-of-prices-for-each-user/15201>\
**Category:** Using the ORM\
**Created:** [August 3, 2022, 6:10am UTC](https://forum.djangoproject.com/t/getting-the-sum-of-prices-for-each-user/15201 "2022-08-03T06:10:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rwahdan2022](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rwahdan2022/32/7584_2.png) [@rwahdan2022](https://forum.djangoproject.com/u/rwahdan2022)\
**Post date:** [August 3, 2022, 6:10am UTC](https://forum.djangoproject.com/t/getting-the-sum-of-prices-for-each-user/15201/1 "2022-08-03T06:10:20Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/6/63760cfa2b60584d16b9c8abecb4dd40b517fcb8.png)  
here is the code:

views.py

```auto
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

```auto
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:

```auto
{% 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?

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [August 3, 2022, 12:07pm UTC](https://forum.djangoproject.com/t/getting-the-sum-of-prices-for-each-user/15201/2 "2022-08-03T12:07:10Z")

</div>

Hey there!  
Take a look into annotate/aggregate on the [docs](https://docs.djangoproject.com/en/4.1/topics/db/aggregation/).

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