Reputation: 126
I have a simple model:
class Atividade(models.Model):
valor_brl = models.DecimalField(max_digits=14, decimal_places=2)
dolar_ref = models.DecimalField(max_digits=14, decimal_places=2)
valor_usd = models.DecimalField(max_digits=14, decimal_places=2)
I create after a method to calculate a new value using the two fields from the model
@property
def valor_usd_novo(self):
valor_usd_novo = self.valor_brl / self.dolar_ref
return valor_usd_novo
If I call {{ valor_usd_novo }}
I get the result I want.
After this I create the save() method, to save valor_usd_novo
result in valor_usd
field from the model.
def save(self, *args, **kwargs):
self.valor_usd = self.valor_usd_novo()
super(Atividade, self).save(*args, **kwargs)
The point is that when I call valor_usd
I got no results and the database do not update with the result from valor_usd_novo
method.
What I missing here?
Upvotes: 0
Views: 45
Reputation: 126
I really appreciate all the help. I found the solution for the problem:
def new_value(self, *args, **kwargs):
self.valor_usd = self.valor_usd_novo
super(Atividade, self).save(*args, **kwargs)
return self.valor_usd
With this code when I call {{atividade.new_value}}
in the template I got the new value and the value in self.valor_usd
update.
Upvotes: 0
Reputation: 41987
valod_usd_novo
is a property
, so you need treat it like an attribute, not a method. Hence, drop the call and refer as an attribute:
def save(self, *args, **kwargs):
self.valor_usd = self.valor_usd_novo # not self.valod_usd_novo()
Upvotes: 1