[Answered ]-Django – Model field property

1👍

Your initial attempt isn’t working because you are ignoring the signature of the property-function.

And then your solution could be improved with the help of the property-decorator:

@property
def total(self):
    return self.percent.percent * self.amount
👤arie

1👍

my problem solved by some property edit:

def _get_total(self):
        # return float(self.percent.percent) * float(self.amount)
        return float(self.percent.percent) * float(self.amount)
    total = property(_get_total)
👤Ehsan

0👍

I think a better solution could be to convert both to decimal (as one is already a Decimal) to avoid float point calculation errors. Pass the percent as a string for the same reason. Therefore this would become:

return Decimal(f'self.percent.percent') * self.amount
👤Alex

Leave a comment