The Python Oracle

Django: Make user email required

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Industries in Orbit Looping

--

Chapters
00:00 Django: Make User Email Required
01:08 Accepted Answer Score 12
01:28 Answer 2 Score 10
01:44 Answer 3 Score 1
02:14 Thank you

--

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

--

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

--

Tags
#python #django

#avk47



ACCEPTED ANSWER

Score 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




ANSWER 2

Score 11


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



ANSWER 3

Score 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)."