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)
- [Answered ]-How to separate a database from a Django web-app when going live?
- [Answered ]-Django REST Framework, working with list of objects after get_queryset
- [Answered ]-Django random sort list
- [Answered ]-Django admin: view hangs on field_sets block
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
- [Answered ]-Django is not displaying the image via /media/
- [Answered ]-Celery .env variables not used in settings.py
- [Answered ]-Request.method == 'POST' is not working in Django
Source:stackexchange.com