Django:如何更改Inline Formset中的字段小部件 [英] Django: How to change a field widget in a Inline Formset

查看:433
本文介绍了Django:如何更改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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆