iJK*_*iJK 1 django django-forms
当我渲染我的formset时,其中一个字段呈现为选择框,因为它是模型中的外部字段.有没有办法将其更改为文本输入?我想通过使用Ajax自动完成来填充该字段.将小部件添加到modelform不起作用,因为modelformset_factory采用模型而不是模型形式.
编辑
我的模型表格
class RecipeIngredientForm(ModelForm):
class Meta:
model = RecipeIngredient
widgets = { 'ingredient' : TextInput(), }
Run Code Online (Sandbox Code Playgroud)
我在我看来使用它
RecipeIngredientFormSet = modelformset_factory(RecipeIngredient, form=RecipeIngredientForm)
objRecipeIngredients = RecipeIngredientFormSet()
Run Code Online (Sandbox Code Playgroud)
编辑模型表格
class RecipeIngredientForm(ModelForm):
ingredient2 = TextInput()
class Meta:
model = RecipeIngredient
Run Code Online (Sandbox Code Playgroud)
我创建这样的表单集
RecipeIngredientFormSet = modelformset_factory(RecipeIngredient, form=RecipeIngredientForm)
objRecipeIngredients = RecipeIngredientFormSet()
Run Code Online (Sandbox Code Playgroud)
题
我必须在html中使用formset吗?我可以硬编码生成的字段并使用javascript我可以创建新字段并增加"form-TOTAL-FORMS"吗?如果可以的话,我不必担心我的模型形式.
谢谢
modelformset_factory确实采用了一种形式.这是来自的函数签名django.forms.models:
def modelformset_factory(
model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
formset=BaseModelFormSet,
extra=1, can_delete=False, can_order=False,
max_num=0, fields=None, exclude=None):
Run Code Online (Sandbox Code Playgroud)
如果这不适合你,请显示一些代码,我会试着看看出了什么问题.
在各种注释之后编辑正如您所指出的,当以这种方式使用时,widget参数是错误的.所以解决方案不是使用它 - 它在任何情况下都是最近添加的.而是直接在表单上定义字段:
class RecipeIngredientForm(forms.ModelForm):
ingredient = forms.ModelChoiceField(widget=forms.TextInput))
class Meta:
model = RecipeIngredient
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1842 次 |
| 最近记录: |