The Python Oracle

Django Crispy form set model field as required

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC O Beethoven - Piano Sonata No 3 in C

--

Chapters
00:00 Question
00:46 Accepted answer (Score 7)
01:08 Thank you

--

Full question
https://stackoverflow.com/questions/4714...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #django #djangoforms #djangocrispyforms

#avk47



ACCEPTED ANSWER

Score 7


You can alter self.fields['email'] in the __init__ method. You need to call super() first.

class RegisterMyBuisinessForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        ...
        super(RegisterMyBuisinessForm, self).__init__(*args, **kwargs)
        self.fields['email'].required = True
        ...