【发布时间】:2022-08-10 03:11:04
【问题描述】:
我使用本教程创建了一个投票应用程序……我一直在扩展它……我已经让它工作了……但我不知道如何将投票转化为百分比。 ..
我试图做类似的事情......
def percentage(self):
return 100 * (self.votes) / (self.survey)
但这不起作用...
我的模型看起来像...
class Choice(models.Model):
choice = models.TextField(max_length=264,blank=True,null=True,unique=False)
survey = models.ForeignKey(\"Survey\",on_delete=models.CASCADE,related_name=\"choice_survey\")
votes = models.IntegerField(default=0)
class Survey(models.Model):
survey_name = models.CharField(max_length=255,blank=True,null=True,unique=False)
survey_type = models.CharField(choices=STATUS_CHOICES8,blank=True,max_length=300)
我见过annotate 的例子,我也玩过它们。我需要跟踪总票数作为属性吗?我见过的其他例子都是外键。通过获取整数字段,我完全可以得到票数。我似乎无法弄清楚如何将其转换为百分比。
在此先感谢您的任何建议。
-
我能够根据之前的 SO 做我想做的事。 stackoverflow.com/questions/18933943/…
标签: python-3.x django django-views django-templates