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?

- 5,842
- 6
- 30
- 45
-
possible duplicate of [Django Inline Formsets using custom form](http://stackoverflow.com/questions/5106327/django-inline-formsets-using-custom-form) – Martey May 29 '15 at 09:25
5 Answers
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})
})

- 18,848
- 11
- 103
- 80
-
1
-
I've resolved with inlineformset_factory(MotherModel, ChildModel, form=MyForm, extra=1) – IlConte Aug 17 '20 at 18:06
-
How do you add widget for the first form, and not the extra ones? – AnonymousUser Apr 18 '22 at 07:06
This is an example of customizing one field using formfield_callback:
def formfield_callback(field):
if isinstance(field, models.ChoiceField) and field.name == 'target_field_name':
return fields.ChoiceField(choices = SAMPLE_CHOICES_LIST, label='Sample Label')
return field.formfield()
FormSet = inlineformset_factory(ModelA, ModelB, extra=1, formfield_callback = formfield_callback)

- 3,547
- 1
- 17
- 24
You need to define a form and update widget in the Meta
class. Look at Overriding the default field types or widgets

- 52,392
- 12
- 90
- 87
-
Thank you for the answer! But how to specify a form to be used in a inline-formset? – nsbm Aug 21 '12 at 12:38
-
@nsbm, you can define a model form and create `formset` (not `modelformset` from it. – Rohan Aug 21 '12 at 12:49
you can subclass the formset and override the add_fields method. This worked for me and I am using Django 1.5 :( .
AuthorInlineFormSet = inlineformset_factory(Author, Book)
class AuthorFormSet(AuthorInlineFormSet):
def add_fields(self, form, index):
super(ReferenceForm,self).add_fields(form,index)
form.fields["name"] = forms.CharField(widget=forms.TextInput())

- 5,960
- 4
- 21
- 27
I know this is a really old thread. Now we can do this using django ModelForm itself.
Add your model form in forms.py
with widgets.
forms.py
class QAForm(forms.ModelForm):
class Meta:
model = QA
fields = ['question', 'answer', 'status']
widgets = {
'question': widgets.Textarea(
attrs={
'class': 'form-control',
'rows': 3,
'cols': 40
}
),
'answer': widgets.Textarea(
attrs={
'class': 'form-control',
'rows': 3,
'cols': 40
}
),
'status': widgets.Select(
attrs={
'class': 'form-select'
}
)
}
pass it using from
parameter for your modelformset_factory
like following in your views.py
views.py
QAFormSet = modelformset_factory(
models.QA,
fields=["question", "answer", "status"],
form=QAForm
)

- 245
- 2
- 7
- 22