How do I go about adding a new class attribute for each for my fields in my formset? I was thinking it would be along these lines of code but the attribute fields does not exist in a formset object.
55 # create the formset
56 ItemUserDetailsFormset = modelformset_factory(CartItemUserDetails,
57 exclude=['cart', 'cart_item'], formset=BaseItemUserDetailsFormset)
37 class BaseItemUserDetailsFormset(BaseModelFormSet):
38 def __init__(self, *args, **kwargs):
39 super(BaseItemUserDetailsFormset, self).__init__(*args, **kwargs)
40 self.fields['first_name'].widgets = forms.TextInput(attrs={"class":"required"})
You’re trying to set the
fieldsattribute on the formset. You need to set it on the form class, not the formset class.So, you want something like…
Sadly, I scanned the docs and didn’t see documentation of the
formkeyword argument in the Django docs, but it’s clearly presented if you look at themodelform_factoryfunction itself (see line 371).Also, if the only thing that you’re changing is the widget property, and if you’re using Django >= 1.2 (which you should be), there’s a slightly simpler syntax on the form itself: