suppose I have two models
class Project(models.Model):
project_number = models.Charfield(primary_key=True, max_length=10)
project_title = models.Charfield(max_length=100)
total_cost = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
class FieldChangeOrder(models.Model):
project = models.FoereignKey('Project', on_delete=models.CASCADE)
funding_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
percentage_of_funding = ( funding_amount / total_cost ) * 100
How do I access total_cost from Project model from FieldChangeOrder model to calculate the value for percentage_of_fundiing field?
I tried the following
class Project(models.Model):
project_number = models.Charfield(primary_key=True, max_length=10)
project_title = models.Charfield(max_length=100)
total_cost = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
class FieldChangeOrder(models.Model):
project = models.FoereignKey('Project', on_delete=models.CASCADE)
funding_amount = models.DecimalField(max_digits=8, decimal_places=2, blank=True, null=True)
@property
def percentage_of_funding(self):
return (self.funding_amount/self.project.total_cost) * 100
but ^ errors out saying FieldChangeOrder object has no attribute 'project'
I am fairly new to django and am just dabbling right now. Any help is much appreciated