[Answered ]-How can I use the information of a field from one model to calculate another field in other model?

1👍

You can access the Ingredient object fields through the ingredient foreign key:

class Recipe(models.Model):
    ...
    def cost_per_ingredient(self):
        cost_per_ingredient = self.ingredient_quantity * self.ingredient_name.cost_per_unit
        return cost_per_ingredient

I would suggest to call it "ingredient" though, not "ingredient_name".

Leave a comment