我需要一种方法来添加(和编辑)元素(与M2M连接)的前端(而不是管理员)形式。
我的模特:
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
的观点:
class addRecipe(CreateView):
form_class = RecipeForm
在第一步中,我尝试使用IngredientFormSet = inlineformset_factory(Ingredient,Recipe, form=RecipeForm)
,但我发现了一个错误:
<class 'recipe.models.Recipe'> has no ForeignKey to <class 'recipe.models.Ingredient'>
这是最好的,可重复使用的方式吗?这个小部件是否存在?