[Fixed]-Setting HTML required attribute in Django formsets

1👍

How about creating formset from MyForm?

MyFormSet = forms.formset_factory(MyForm)

0👍

After spending three hours, I’ve solved the issue by setting a custom form in modelformset_factory. Maybe it will be useful for someone else

MyFormSet =  modelformset_factory(MyModel, MyForm)
formset = MyFormSet(queryset = MyModel.objects.filter(...))

Specifying MyForm effectively tells Django to inherit all widget attributes that you have once declared in the MyForm definition.

Using formset_factory is for some reasons a headache for me, primarily because it accepts values instead of querysets which means I have to bother about foreign key relationships.

Leave a comment