Insert attributes in all fields in a ModelForm Django 1.8

0

Hello friends, I'm having a problem with a modelForm in django 1.8, the code is as follows:

class TrabajoForm(forms.ModelForm):
    class Meta:
        model = Trabajo
        exclude = ['fecha_solicitud', 'revisado']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields:
            fields[field].widget.attrs.update({'class': 'form-control'})

What I want is to give a css class to all the fields of the form, according to the error that Django gives me when using it, the 'self' is not defined, what can I do?

Thank you very much !!! :)

    
asked by Carlos Vega 24.07.2016 в 18:59
source

1 answer

0

You must add your own class and self when invoking the super() method. Something like this:

class TrabajoForm(forms.ModelForm):
    class Meta:
        model = Trabajo
        exclude = ['fecha_solicitud', 'revisado']

    def __init__(self, *args, **kwargs):
        super(TrabajoForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            self.fields[field].widget.attrs.update({'class': 'form-control'})

You should also refer to self.fields every time you call it, even if it is within the for cycle.

    
answered by 24.07.2016 / 19:15
source