been trying to save my recipe-ingredients and ingredient-measure to my recipe db.
i tried a model like this:
class IngredientList(models.Model):
unit = [
('lb', 'pound'),
('oz', 'ounce'),
('tsp', 'teaspoon'),
('tbsp', 'tablespoon'),
('G', 'gallon'),
('ml', 'mililitre'),
('l', 'litre'),
('g', 'gram'),
('kg', 'kilogram'),
]
name = models.CharField(max_length=100)
image = models.ImageField(default='img.jpg')
#recipes = models.ManyToManyField('Recipe', through='RecipeList')
protein = models.FloatField()
fat = models.FloatField()
carb = models.FloatField()
units = models.CharField(max_length=10, choices=unit)
def __str__(self):
return self.name
return self.ingredient.name
class Recipe(models.Model):
name = models.CharField(max_length=100)
ingredients = models.ManyToManyField(
'IngredientList', through='RecipeList')
instructions = models.TextField(max_length=3000)
serving_size = models.IntegerField()
image = models.ImageField(default='recipe.jpg')
duration = models.IntegerField(blank=True, default=0)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('recipe-detail', kwargs={'pk': self.pk})
class RecipeList(models.Model):
recipe = models.ForeignKey(RecipeList, on_delete=models.CASCADE)
ingredients = models.ForeignKey(IngredientList, on_delete=models.CASCADE)
measurement = models.FloatField()
def __str__(self):
return self.recipe.name
Found this form on a stackoverflow answer:
class RecipeForm(forms.Form):
name = forms.CharField()
ingredient = forms.ModelMultipleChoiceField(IngredientList.objects.all())
instructions = forms.CharField()
count = forms.IntegerField()
serving = forms.IntegerField()
and this CreateView as well:
class RecipeCreateView(FormView):
template_name = "recipe/shoppinglist_form.html"
form_class = RecipeForm
def form_valid(self, form):
name = form.cleaned_data['name']
ingredient = form.cleaned_data['ingredient']
instructions = form.cleaned_data['instructions']
serving = form.cleaned_data['serving']
recipe = RecipeList(name=name)
recipe.save()
ingredient_list = IngredientList.objects.filter(pk__in=ingredient)
for i in ingredient_list:
recipe.ingredients.add(i)
return render(request, 'recipe/recipe.html')
When I submit, yes I do get the ingredients and the name of the recipe. However i am unable to state the measurement of each ingredient, and also the other details aren't being filled ( serving_size, instructions...)
Please someone help!