[Django]-Django: Make user email required

12👍

I have to answer my question because now I know the solution:

The way I described in the start post should work. The docs are just wrong:
https://code.djangoproject.com/ticket/29192

8👍

add in models.py

from django.contrib.auth.models import User

User._meta.get_field('email')._unique = True
User._meta.get_field('email').blank = False
User._meta.get_field('email').null = False

1👍

Edit: You have to subclass AbstractBaseUser and implement all other attributes too.

from django.contrib.auth.models import AbstractBaseUser, UserManager

class User(AbstractBaseUser):
    email = models.EmailField(_('email address'), blank=False)       
    objects = UserManager()

Second edit:

Aliquis answer is the correct one. Current documentation is simply wrong: “It is an error to have fields in the abstract base class with the same name as those in the child (and Django will raise an exception).”

👤jazz

Leave a comment