gpt4 book ai didi

Django 模型 : Multiplication of two fields

转载 作者:行者123 更新时间:2023-12-05 00:47:35 29 4
gpt4 key购买 nike

我想将模型中的两个字段相乘并将其显示在模板中。我有两个模型:

class Priority(models.Model):
priorityName=models.CharField(max_length=100)
priorityScore=models.IntegerField()
def __str__(self):
return str(self.priorityScore)

class ImpactMatrix(models.Model):
objectName=models.ForeignKey(ObjectName,on_delete=models.CASCADE)
actionName=models.ForeignKey(ActionObject,on_delete=models.CASCADE)
priority=models.ForeignKey(Priority,on_delete=models.CASCADE,related_name='priority')
functional=models.ForeignKey(Priority,on_delete=models.CASCADE,related_name='functional')
supervision=models.ForeignKey(Priority,on_delete=models.CASCADE,related_name='supervision')
approval=models.ForeignKey(Priority,on_delete=models.CASCADE,related_name='approval')

我想乘以 ImpactMatrix.priority * ImpactMatrix.functional 中的 priorityScore

我该怎么做呢?

最佳答案

定义一个 @property 在模型中并像任何其他模型字段一样从模板访问它,

class ImpactMatrix(models.Model):
objectName = models.ForeignKey(ObjectName, on_delete=models.CASCADE)
actionName = models.ForeignKey(ActionObject, on_delete=models.CASCADE)
priority = models.ForeignKey(Priority, on_delete=models.CASCADE, related_name='priority')
functional = models.ForeignKey(Priority, on_delete=models.CASCADE, related_name='functional')
supervision = models.ForeignKey(Priority, on_delete=models.CASCADE, related_name='supervision')
approval = models.ForeignKey(Priority, on_delete=models.CASCADE, related_name='approval')

@property
def some_name(self):
return self.priority.priorityScore*self.functional.priorityScore

在你的情调中,
{% impactmatrix_object.some_name %}

关于Django 模型 : Multiplication of two fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587150/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com