Hey all!
I thought I was getting a hang of the django ORM but I can’t figure out how this query is supposed to be done.
My query involves the following three models:
class Recipe(models.Model):
name = models.CharField(max_length=100)
servings = models.FloatField(default=1)
class Ingredient(models.Model):
name = models.CharField(max_length=100)
class RecipeLine(models.Model):
parent_recipe = models.ForeignKey(Recipe, related_name='recipe_lines', on_delete=models.CASCADE)
ingredient = models.ForeignKey(Ingredient, related_name='ingredient', on_delete=models.SET_NULL, blank=True, null=True)
number = models.FloatField(blank=True, default=0)
unit = models.CharField(max_length=100, blank=True, default='')
class IngredientAlias(models.Model):
parent_ingredient = models.ForeignKey(Ingredient, related_name='aliases', on_delete=models.CASCADE)
alias = models.CharField(max_length=100)
In my app a user navigates to a recipe page that displays a recipe
with all the associated recipe_lines
which all have associated ingredients
. What I’d like to do is gather all the ingredient_aliases
for all the ingredients
for all the recipe_lines
given a recipe
.
The raw SQL of this query looks like this:
select ingredientalias.alias, ingredientalias.id, ingredientalias.parent_ingredient_id
from ingredientalias
inner join ingredient on ingredientalias.parent_ingredient_id = ingredient.id
inner join recipeline on recipeline.ingredient_id = ingredient.id
inner join recipe on recipe.id = recipeline.parent_recipe_id
where recipe.id = '{recipe.id}'
As of now I’m using .raw()
for this particular endpoint but I’d really like to know if/how this can be done with the ORM.
thank you and please let me know if you need any more information!