Another follow up question to Creating a Django Model for a recipe #2.
I've run into an error with the following code:
class Ingredient(models.Model):
'''All ingredients for any recipe or individual selection'''
name = models.CharField(max_length=50)
department = models.ForeignKey(Department, null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.ingredient
class IngredientDetail(models.Model):
'''A class to identify variable quantities of ingredients'''
ingredient = models.ForeignKey(Ingredient, null=True, on_delete=models.SET_NULL)
quantity = models.CharField(max_length=1)
class Meta:
verbose_name_plural = 'Ingredient Details'
def __str__(self):
return self.quantity
class Recipe(models.Model):
'''A Recipe class to input new recipes'''
recipe = models.CharField(max_length=50)
ingredients = models.ManyToManyField(Ingredient, through=IngredientDetail)
instructions = models.TextField(null=True)
cuisine = models.ForeignKey(Cuisine, null=True, on_delete=models.SET_NULL)
picture = models.ImageField(upload_to= 'media/', null=True, blank=True)
def __str__(self):
return self.recipe
I've set it up this way to associate variable quantities of ingredients with multiple recipes. However, when I run this code, the following issue is identified by Django:
grocery_lists.IngredientDetail: (fields.E336) The model is used as an intermediate model by 'grocery_lists.Recipe.ingredients', but it does not have a foreign key to 'Recipe' or 'Ingredient'.
As you can see, there IS a foreign key to 'Ingredient', so I'm not sure whats wrong.