16

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 django.contrib.auth.forms import UserCreationForm

for my User creation form, and combining this with my own custom UserProfileCreateForm

Ian

IanSR
  • 1,415
  • 3
  • 16
  • 18

3 Answers3

22

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
Derek Reynolds
  • 3,473
  • 3
  • 25
  • 34
7
from django import forms
from django.contrib.auth.models import User


class MyUserForm(forms.ModelForm):

    email = forms.CharField(max_length=75, required=True)

    class Meta:

        model = User
        fields = ('username', 'email', 'password')
Dmitry
  • 2,068
  • 2
  • 21
  • 30
0

use EmailField in your model

see more at https://docs.djangoproject.com/en/2.1/ref/models/fields/#emailfield

Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88