Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

489
Visualizações
Django: Make user email required

For my application the email field of a User should be required. That's not the case for the default User model. So I thought it would make sense to create a custom user model as described in the docs:

If you’re starting a new project, it’s highly recommended to set up a custom user model, even if the default User model is sufficient for you. This model behaves identically to the default user model, but you’ll be able to customize it in the future if the need arises:

from django.contrib.auth.models import AbstractUser

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

However, I am not sure if that's the correct way to achieve this.

The reason is that here the Django docs say:

Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class. 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).

What should I do now?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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
over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda