繁体   English   中英

在 Django 模型中执行算术运算后,如何使用获得的新值更新模型中整数的值?

[英]After performing arithmetic operations within a django model, how can I update the value of an integer in my model using the new value I obtained?

我下面的代码来自这个问题

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}'

基本上,我想将用户输入的金额乘以我使用 get_crypto_price 函数获得的价​​格(我已经确认 get_crypto_price 函数有效)。 保存 self.price_with_amount 后,我​​想在我的str方法中返回它,然后将它传递给我的 views.py 以在我的 HTML 中使用。 例如,当我将 price_with_amount 的值设为 1 时,就像我在代码中所做的那样,它会通过并在我的 HTML 中正常工作。 我想要做的是将 price_with_amount 的值更改为方法calculate_price 中获得的值。 如何在保持我目前拥有的方法的同时做到这一点?

谢谢 :)

如果你真的想把它保存到数据库中,你可以使用editable=False参数使该字段成为一个models.FloatField ,这样它就不会出现在用户编辑的表单中。 然后您的其余代码应该按原样工作。

但...

由于price_with_amount是基于当前价格的动态值,因此通过get_crypto_price函数保存它似乎不是一个好主意。

相反,您可以使用@property即时计算值。 property是一个实例方法,您可以像使用常规属性一样使用它。

你的代码可以这样重写:

class CryptoData(models.Model):
    currency = models.CharField(...)
    amount = models.IntegerField()

    ...

    @property
    def price_with_amount(self):
        return self.calculate_price()

    def __str__(self):
        return f'{self.currency}-{self.amount}-{self.price_with_amount}'

使用此实现,您始终可以访问CryptoData实例上的price_with_amount属性,只要它具有currencyamount值,它就会显示按需计算的值。

>>> crypto = CryptoData(currency="ETH", amount=10)
>>> crypto.price_with_amount
22718.20
>>> crypto.save()

# It still behaves as expected after fetching it from the db
>>> CryptoData.objects.get(id=crypto.id).price_with_amount
22718.20

# You can use the property just like you would a regular attribute
>>> if crypto.price_with_amount > 1000:
        print("foobar")
foobar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM