I need a way to add (and edit) elements (connected with M2M) in a frontend (not admin) form.
My Models:
class IngredientName(models.Model):
name = models.CharField(_('name of ingredient'), max_length=255)
class Unit(models.Model):
name = models.CharField(_('unit name'), unique=True, max_length=255)
class Ingredient(models.Model):
name = models.ForeignKey(IngredientName)
unit = models.ForeignKey(Unit)
value = models.FloatField()
class Recipe(models.Model):
title = models.CharField(_('title'), max_length=250)
portions = models.PositiveIntegerField(_('portions'))
ingredients = models.ManyToManyField(Ingredient)
description = models.TextField(_('description'))
Forms:
class RecipeForm(ModelForm):
class Meta:
model = Recipe
views:
class addRecipe(CreateView):
form_class = RecipeForm
In a first step I've tried to use IngredientFormSet = inlineformset_factory(Ingredient,Recipe, form=RecipeForm)
but i just got an error:
<class 'recipe.models.Recipe'> has no ForeignKey to <class 'recipe.models.Ingredient'>
Whats the best, and reusable way, to do this? does a widget exist for this?