# mathematical operations between Model Fields

**URL:** <https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517>\
**Category:** Using the ORM\
**Created:** [May 19, 2020, 12:32am UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517 "2020-05-19T00:32:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FoxRio](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@FoxRio](https://forum.djangoproject.com/u/FoxRio)\
**Post date:** [May 19, 2020, 12:32am UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/1 "2020-05-19T00:32:40Z")

</div>

Hi,

I’m trying to build a swimming pool water test app as my first project but stumbled on a problem - I don’t know how to do mathematical operations between model fields. So, I have a function in models.py:

```
class PoolTest(models.Model):
    location = models.CharField(max_length=50)
    freecl = models.DecimalField(max_digits=5, decimal_places=2)
    totalcl = models.DecimalField(max_digits=5, decimal_places=2)

```

Then I want to display it in .html file with:

```
Location: {{ pooltest.location }}<br>
Free Cl: {{ pooltest.freecl }}<br>
Total Cl: {{ pooltest.totalcl }}<br>

```

But I also need to show Combined Cl after Total Cl:, which is **Combined Cl = Total Cl - Free Cl**

How can I get it done, please?

Updated: There is my views.py

```
from django.shortcuts import render
from pooltest.models import PoolTest

def pooltest(request):
    all_pooltests = PoolTest.objects.all()
    return render(request, 'pooltest/home.html', {'all_pooltests':all_pooltests})

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 19, 2020, 1:38am UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/2 "2020-05-19T01:38:35Z")

</div>

In your view that is generating your context being rendered, you can add another variable that is then included in your template.

(Can’t be more specific without seeing your view.)

---

<div class="post-metadata">

**Author:** ![FoxRio](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@FoxRio](https://forum.djangoproject.com/u/FoxRio)\
**Post date:** [May 19, 2020, 9:18am UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/3 "2020-05-19T09:18:43Z")

</div>

I tried that, but it didn’t work, obviously I used wrong syntax… I updated topic with my view.py file.  
What would be correct way to do it?

Thank you,

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 19, 2020, 1:00pm UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/4 "2020-05-19T13:00:37Z")

</div>

Ok, so what you’re looking for is to calculate this “combined Cl” for each row in your table.

The function you’re looking for is the [annotate](https://docs.djangoproject.com/en/3.0/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset) function.

In this case, you could do something like this:

```
all_pooltests = PoolTest.objects.annotate(combined_cl = F('totalcl') - F('freecl'))

```

You will then be able to reference that annotated field in your template as `pooltest.combined_cl`

If you need to perform more complex calculations in your query, Django supports a set of [database functions](https://docs.djangoproject.com/en/3.0/ref/models/database-functions/#module-django.db.models.functions) that can also be used.

Ken

---

<div class="post-metadata">

**Author:** ![FoxRio](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@FoxRio](https://forum.djangoproject.com/u/FoxRio)\
**Post date:** [May 19, 2020, 1:49pm UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/5 "2020-05-19T13:49:20Z")

</div>

Works, thank you!  
I will definitely read more about database functions.

---

<div class="post-metadata">

**Author:** ![b0nafideCicer0](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/b0nafidecicer0/32/24565_2.png) [@b0nafideCicer0](https://forum.djangoproject.com/u/b0nafideCicer0)\
**Post date:** [October 12, 2024, 6:25pm UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/6 "2024-10-12T18:25:34Z")

</div>

hello, i am new to django and i am enjoying it. i needhelp with a trivial problem- i have a django model with items that have unit\_price and quantity and total. i want the total to be calculated automatically on the admin site after i insert the unit\_price and the quantity. how to implement

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 12, 2024, 6:58pm UTC](https://forum.djangoproject.com/t/mathematical-operations-between-model-fields/2517/7 "2024-10-12T18:58:42Z")

</div>

Welcome @b0nafideCicer0 !

Are you talking about changing it on the page **before** saving those values? Or are you talking about clicking “save” and having that process update what’s shown on the page?
