Home > database >  Django model property field calculated with field from another child model
Django model property field calculated with field from another child model

Time:10-10

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

I have code as above and would like to add property of final rating using calculation from own table(TimeImpact) field final_score and field from CriticalImpact field ie percentage. The problem is how i can get the field percentage from CriticalImpact table which is the child of business function table. I've searched before but most of the answer is calculated field coming from field in own table and parent table, not with another child table.

CodePudding user response:

Currently, you have no relation, make ForeignKey in TimeImpact model which would have Many-to-One relation with CriticalImpact model, then you can take percentage field via chaining, so:

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
  • Related