Django: How to change a field widget in a Inline Formset

后端 未结 4 1016
别那么骄傲
别那么骄傲 2021-02-13 07:10

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

4条回答
  •  终归单人心
    2021-02-13 07:52

    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})
    })
    

    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})
    })
    

提交回复
热议问题