Django:如何更改Inline Formset中的字段小部件 [英] Django: How to change a field widget in a Inline Formset
问题描述
我是Django的新手,我认为我在文档中缺少这个。
问题是在inline-formset中,我不会声明一个表单,只需传递两个模型即可构造它。
我想知道如何使用内联表单来更改单个字段的小部件?
I am new to Django and I think I am missing this in the docs.
The problem is that in inline-formset I dont declare a form, just pass two models to construct it.
I want to know how can I change a widget of a single field using inline formset?
推荐答案
从Django 1.6 开始,您可以使用小部件
参数 modelformset_factory
,以便自定义特定字段的小部件:
As of Django 1.6, you can use the widgets
parameter of modelformset_factory
in order to customize the widget of a particular field:
AuthorFormSet = modelformset_factory(Author, widgets={
'name': Textarea(attrs={'cols': 80, 'rows': 20})
})
和 inlineformset_factory (使用 modelformset_factory
and therefore the same parameter for inlineformset_factory
(which uses modelformset_factory
):
AuthorInlineFormSet = inlineformset_factory(Author, Book, fields=['name'], widgets={
'name': Textarea(attrs={'cols': 80, 'rows': 20})
})
这篇关于Django:如何更改Inline Formset中的字段小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!