【发布时间】:2011-03-03 01:35:02
【问题描述】:
当我呈现我的表单集时,其中一个字段呈现为一个选择框,因为它是模型中的一个外来字段。有没有办法将其更改为文本输入?我想使用 Ajax 自动完成来填充该字段。向模型表单添加小部件不起作用,因为 modelformset_factory 采用模型而不是模型表单。
编辑
我的模型表格
class RecipeIngredientForm(ModelForm):
class Meta:
model = RecipeIngredient
widgets = { 'ingredient' : TextInput(), }
我在我的观点中使用它
RecipeIngredientFormSet = modelformset_factory(RecipeIngredient, form=RecipeIngredientForm)
objRecipeIngredients = RecipeIngredientFormSet()
编辑模型表格
class RecipeIngredientForm(ModelForm):
ingredient2 = TextInput()
class Meta:
model = RecipeIngredient
我创建这样的表单集
RecipeIngredientFormSet = modelformset_factory(RecipeIngredient, form=RecipeIngredientForm)
objRecipeIngredients = RecipeIngredientFormSet()
问题
我必须在 html 中使用表单集吗?我可以对生成的字段进行硬编码并使用 javascript 创建新字段并增加“form-TOTAL-FORMS”吗?如果可以的话,我就不必担心我的模型形式了。
谢谢
【问题讨论】:
-
看起来这是一个错误code.djangoproject.com/ticket/13095。如何应用补丁?
标签: django django-forms