Override BaseInline Formset
<p>The code you provided seems to define a custom formset called <code>BootstrapFormSet</code> that inherits from Django's <code>BaseInlineFormSet</code>. This custom formset is intended to add the 'form-control' class to form fields' …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | from django.forms import BaseInlineFormSet
class BootstrapFormSet(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
fields = ['CheckboxInput', 'ClearableFileInput', 'FileInput']
for form in self.forms:
for field_name, field in form.fields.items(): # Corrected iteration to access field instances
widget_name = field.widget.__class__.__name__
if widget_name not in fields:
field.widget.attrs.update({
'class': 'form-control'
})
def add_fields(self, form, index):
super().add_fields(form, index)
fields = ['CheckboxInput', 'ClearableFileInput', 'FileInput']
for field_name, field in form.fields.items(): # Corrected iteration to access field instances
widget_name = field.widget.__class__.__name__
if widget_name not in fields:
field.widget.attrs.update({
'class': 'form-control'
})
|