I have in models.py:
class Team(models.Model):
x = models.IntegerField()
y = models.IntegerField()
a = models.IntegerField()
b = models.IntegerField()
def get_stat(self):
return {
'xy': self.x + self.y,
'ab': self.a + self.b
}
stat = property(get_stat)
In team.html I have:
xy stat: {{ team.stat.xy }}
ab stat: {{ team.stat.ab }}
The question is: “does django executes get_stat function everytime I call stat or it caches result?”
No,
propertydoes not cache any results.If you wanted it cached for heavy repeated access, the typical design pattern is:
Of course, this doesn’t work if are doing something odd like dealing with instances that are modified in place, but not commited to the DB. I belive 99% of uses involve DB->Object->Action