The code I have below was derived from this Question
class CryptoData(models.Model):
currency = models.CharField(max_length=20, choices=currency_choices, default='BTC')
amount = models.IntegerField()
price_with_amount = 1
def calculate_price(self):
if self.currency == "BTC":
currency_price = get_crypto_price("bitcoin")
elif self.currency == "ETH":
currency_price = get_crypto_price("ethereum")
elif self.currency == "UNI":
currency_price = get_crypto_price("uniswap")
elif self.currency == "ADA":
currency_price = get_crypto_price("cardano")
elif self.currency == "BAT":
currency_price = get_crypto_price("basic attention token")
price_with_amount = currency_price * self.amount
return price_with_amount
def save(self,*args,**kwargs):
self.price_with_amount = self.calculate_price()
super().save(*args, **kwargs)
class Meta:
verbose_name_plural = "Crypto Data"
def __str__(self):
return f'{self.currency}-{self.amount}-{self.price_with_amount}'
Basically, I want to multiply the user input, amount, by the price I obtain using my get_crypto_price function (I have confirmed that the get_crypto_price function works). After saving self.price_with_amount, I want to return it in my str method then pass it to my views.py to be used in my HTML. When I give price_with_amount a value of 1 for example, as I did in my code, it gets passed and works fine in my HTML. What I'm trying to do is change the value of price_with_amount to the obtained values in the method calculate_price. How can this be done while keeping the methods I currently have?
Thanks :)