-2

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

  • You have a Foreign Key relationship between these two models. What research have you done on how to access data in other models that have relationships to each other? – dfundako Nov 12 '19 at 20:25
  • @dfundako I just updated the summary with what I've tried. Please have a look. – aravind marthineni Nov 12 '19 at 20:41
  • @aravindmarthineni Your edited version should work fine, did you forget `make migrations` and `migrate` after creating the model? – Pedram Parsian Nov 12 '19 at 21:04
  • @PedramParsian for some reason, makemigrations comes back with no changes detected! could it be because this change is not actually creating a column in table but merely using a field from other model? – aravind marthineni Nov 12 '19 at 21:18
  • `makemigrations` only takes models from **installed** apps and apply them. Did you include your app name inside `INSTALLED_APPS` in `settings.py`? – Pedram Parsian Nov 12 '19 at 21:23
  • I have it in there – aravind marthineni Nov 12 '19 at 21:24
  • I also realized that there is some typos in your code: `FoereignKey` instead of `ForeignKey` and `Charfield` instead of `Charfield`. But they cause error if you have them in your code... I tested the code and that works just fine. – Pedram Parsian Nov 12 '19 at 21:30
  • My code was free from typos you mentioned but there was something else I fixed and it worked now. However, I looked at the tables after running migrate and percentage_of_funding was not created in the table. I don't think this needed a db migration to take effect. Thank you so much for your help on this @PedramParsian – aravind marthineni Nov 12 '19 at 22:02

1 Answers1

1

Simply, you can use dot notation to access fields on foreign keys:

as an example:

field_changer_order = FieldChangeOrder.objects.get(pk=1)
project_total_cost = field_changer_order.project.total_cost
Pedram Parsian
  • 3,750
  • 3
  • 19
  • 34