How can I get the proper “sumvalue” in the example? (which I only know how to represent in SQL):
JavaScript
x
14
14
1
class Addition(models.Model):
2
location = models.ForeignKey(Place, )
3
a = models.DecimalField( )
4
b = models.DecimalField( )
5
6
@property
7
def c(self):
8
return self.a + self.b
9
10
class Result(models.Model):
11
place = models.ForeignKey(Place, )
12
# I only know how to represent in SQL
13
sumvalue = SELECT c FROM Addition WHERE location = place
14
Advertisement
Answer
In your case, c is not a field, but a property.
You can call it on an instance:
JavaScript
1
2
1
Addition.objects.all()[0].c()
2
Now for what you’re trying to do, I really recommand you use annotate.
JavaScript
1
4
1
from django.db.models import F
2
3
Addiction.objects.annotate(sum_a_b=F('a')+F('b'))
4
You then have an additional field in your query called sum_a_b
.