Django User model email field: how to make it mandatory

前端 未结 3 1692
余生分开走
余生分开走 2020-12-17 15:56

I need to make the email field in the Django User model mandatory. It isn\'t obvious to me how to do that. Suggestions welcome. I am currently using:

from         


        
3条回答
  •  一个人的身影
    2020-12-17 16:31

    You should be able subclass the provided registration form and override properties of a field in the Meta class.

    from django.contrib.auth.forms import UserCreationForm
    
    # Not sure about the syntax on this one. Can't find the documentation.
    class MyUserCreationForm(UserCreationForm):
    
        class Meta:
            email = {
                'required': True
            }
    
    
    # This will definitely work
    class MyUserCreationForm(UserCreationForm):
    
        def __init__(self, *args, **kwargs):
            super(MyUserCreationForm, self).__init__(*args, **kwargs)
    
            self.fields['email'].required = True
    

提交回复
热议问题