Form object has no attribute 'fields' forms.Form django

+1 vote
103 views
asked Apr 13 by anonymous

I am getting this error while accessing form.Forms fields in init.

my form look like this.

class AnswerForm(forms.Form):
    answer = forms.CharField(label="Answer to be posted")

    def __init__(self, *args, **kwargs):
        self.fields['answer'].required = True
        super(AnswerForm, self).__init__(*args, **kwargs)

I am getting

'AnswerForm' object has no attribute 'fields'

What i am doing wrong , can i not access field when using form.Form or something else happening

1 Answer

0 votes
answered Apr 13 by anonymous

Just move super before accessing self.fields.

class AnswerForm(forms.Form):

answer = forms.CharField(label="Answer to be posted")

def __init__(self, *args, **kwargs):
    super(AnswerForm, self).__init__(*args, **kwargs)
    self.fields['answer'].required = True
    
Welcome , qPocho is an online community for programmers share there knowledge.
...