英文:
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)
<b>@property</b>
def total_amount(self):
return self.euros + self.rubles + self.dollars</code></pre>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论