[英]How do I calculate percentages from two tables in Django
I m pretty new to the Django Framework and I am stuck at calculating a percentage. Here
我
m pretty new to the Django Framework and I am stuck at calculating a percentage. Here
m pretty new to the Django Framework and I am stuck at calculating a percentage. Here
s the problem: m pretty new to the Django Framework and I am stuck at calculating a percentage. Here
是问题所在:
I have two tables SocialCase and Donation:我有两张表 SocialCase 和 Donation:
class SocialCase(models.Model): class SocialCase(models.Model):
title = models.CharField(max_length=200)
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
organizer = models.TextField(max_length=200, null=True, blank=False)
description = models.TextField(null=True, blank=False)
created = models.DateTimeField(auto_now_add=True)
profile_image = models.ImageField(upload_to='static/images/')
case_tags = models.ManyToManyField('events.Tag')
target_donation = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
class Donation(models.Model): class 捐赠(型号.型号):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
social_case = models.ForeignKey(SocialCase, on_delete=models.CASCADE)
raised = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
I want to use a @property method to calculate the percentage between raised and target_donation我想使用@property 方法来计算raise和target_donation之间的百分比
I want to calculate the percentage on the SocialCase model.我想计算 SocialCase model 的百分比。 How do I bring the raised column from Donations model in order to calculate the percentage of each Social Case and output it in the HTML template?
我如何将捐赠 model 中的凸起列用于计算 HTML 模板中每个社会案例和 output 的百分比?
Thank you verry much, and and sorry if this a simple question, I m still a newbie and couldn
t find anything in the Django Documentation.非常感谢,如果这是一个简单的问题,我很抱歉,我
m still a newbie and couldn
在 Django 文档中找不到任何内容。
Kind regards, Sergiu亲切的问候,塞尔吉乌
When you define a ForeignKey
it creates a "reverse" field on the related object.当您定义
ForeignKey
时,它会在相关的 object 上创建一个“反向”字段。 You can name this using related_name
, otherwise it defaults to <modelname>_set
(modelname is lowercased).您可以使用 related_name 来命名它,否则它默认为 <
related_name
<modelname>_set
(modelname 是小写的)。 In this case, donation_set
在这种情况下,
donation_set
That's probably what you were missing.这可能就是你所缺少的。 The code will be something like
代码将类似于
@property
def percent_raised(self):
total = 0.0
for donation in self.donation_set.all():
total += float( donation.raised)
return total / float( self.target_donation) * 100.0
It's more efficient in this case but much less generalizable, to calculate the sum of donations in the DB query using an aggregation function.在这种情况下,使用聚合 function 计算 DB 查询中的捐赠总和会更有效,但可推广性要低得多。 See the cheat sheet here (third example using
Avg
, but in this case you'd want Sum
not Avg
)请参阅此处的备忘单(使用
Avg
的第三个示例,但在这种情况下,您需要Sum
而不是Avg
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.