Django中对象字段的总和

huangapple go评论59阅读模式
英文:

Sum of object fields in django

问题

class Bet(models.Model):
    bet_id = models.AutoField(primary_key=True)
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='bet_event')
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    euros = models.IntegerField(default=0)
    rubles = models.IntegerField(default=0)
    dollars = models.IntegerField(default=0)
    total_amount = models.IntegerField(default=0)
    max_amount = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        balance = {'euros', 'rubles', 'dollars'}
        self.total_amount = sum([value for key, value in self.__dict__.items() if key in balance])
        super(Bet, self).save(*args, **kwargs)
英文:

I have a "Bet" model, there is a total_amount field. I want that when creating a model object, 'total_amount' is the sum of the fields "rubles", "euros", "dollars". But when i create new object then 'total_amount' is 0

class Bet(models.Model):
    bet_id = models.AutoField(primary_key=True)
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='bet_event')
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    euros = models.IntegerField(default=0)
    rubles = models.IntegerField(default=0)
    dollars = models.IntegerField(default=0)
    total_amount = models.IntegerField(default=0)
    max_amount = models.IntegerField(default=0)

    def save(self, *args, **kwargs):
        balance = {'euros', 'rubles', 'dollars'}
        self.total_amount = sum([value for key, value in self.__dict__.items() if key in balance])
        super(Bet, self).save(*args, **kwargs)

答案1

得分: 1

请不要存储总和。这是重复的数据。这会使维护一致性变得更加困难。实际上,如果您通过查询更新了dollars,例如,它不会更新total_amount

您可以使用属性代替:

class Bet(models.Model):
    bet_id = models.AutoField(primary_key=True)
    event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='bet_event')
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    euros = models.IntegerField(default=0)
    rubles = models.IntegerField(default=0)
    dollars = models.IntegerField(default=0)
    max_amount = models.IntegerField(default=0)

    @property
    def total_amount(self):
        return self.euros + self.rubles + self.dollars

话虽如此,我不知道将金额相加是否有太多意义。将美元与欧元相加并不会给我们两美元或两欧元,那只是一欧元和一美元。

英文:

Please don't store the sum. This is duplicated data. It makes it harder to maintain consistency. Indeed, if you update dollars for example through a query, it will not update the total_amount.

You can use a property instead:

<pre><code>class Bet(models.Model):
bet_id = models.AutoField(primary_key=True)
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='bet_event')
user = models.ForeignKey(User, on_delete=models.CASCADE)
euros = models.IntegerField(default=0)
rubles = models.IntegerField(default=0)
dollars = models.IntegerField(default=0)
max_amount = models.IntegerField(default=0)

&lt;b&gt;@property&lt;/b&gt;
def total_amount(self):
    return self.euros + self.rubles + self.dollars&lt;/code&gt;&lt;/pre&gt;

That being said, I don't know if summing up amounts makes much sense. Summing up a dollar with a euro does not give us two dollars or two euros, that simply is a euro and a dollar.

huangapple
  • 本文由 发表于 2023年4月11日 15:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定