class BusinessFunction(models.Model):
name = models.CharField(max_length=200)
priority_rating = models.PositiveIntegerField(null=True, blank=True)
location = models.CharField(max_length=200)
network_service_related = models.CharField(max_length=200)
class Meta:
ordering = ['name']
def __str__(self):
return self.name
class CriticalImpact(models.Model):
businessfunction = models.ForeignKey(BusinessFunction, on_delete=models.CASCADE)
priority = models.PositiveSmallIntegerField(null=True, blank=True)
financial = models.PositiveSmallIntegerField(null=True, blank=True)
legal = models.PositiveSmallIntegerField(null=True, blank=True)
score = models.PositiveSmallIntegerField(null=True, blank=True)
percentage = models.PositiveIntegerField(null=True, blank=True)
class Meta:
ordering = ['businessfunction']
class TimeImpact(models.Model):
businessfunction = models.ForeignKey(BusinessFunction, on_delete=models.CASCADE)
impact_score_financial = models.DecimalField(max_digits=5, decimal_places=2, blank=True,
null=True)
impact_score_asset = models.DecimalField(max_digits=5, decimal_places=2,
blank=True, null=True)
final_score = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
class Meta:
ordering = ['businessfunction']
@property
def final_rating(self):
final_rating = (self.final_score ????????)/2
return final_rating
我有上面的代碼,并想使用來自自己的表(TimeImpact)欄位 final_score 和來自 CriticalImpact 欄位的欄位(即百分比)的計算來添加最終評級的屬性。問題是我如何從關鍵影響表中獲取欄位百分比,該表是業務功能表的子表。我以前搜索過,但大多數答案是來自自己表和父表中的欄位的計算欄位,而不是另一個子表。
uj5u.com熱心網友回復:
目前,您沒有關系,ForeignKey在TimeImpact模型中創建與模型具有多對一關系的CriticalImpact模型,然后您可以percentage通過鏈接獲取欄位,因此:
class TimeImpact(models.Model):
businessfunction = models.ForeignKey(BusinessFunction, on_delete=models.CASCADE)
impact_score_financial = models.DecimalField(max_digits=5, decimal_places=2, blank=True,
null=True)
impact_score_asset = models.DecimalField(max_digits=5, decimal_places=2,
blank=True, null=True)
final_score = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
critical_impact=models.ForeignKey(CriticalImpact,on_delete=models.CASCADE)
class Meta:
ordering = ['businessfunction']
@property
def final_rating(self):
final_rating = (self.final_score self.critical_impact.percentage)/2
return final_rating
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/512077.html
標籤:Pythondjangodjango模型外键django-model-field
