[Python] When and where i use create_user from CreateUserManager()?

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    For several days now, I've been wondering why I should create my own user manager in Django, since in the end, when inheriting a built-in form, I don't refer to my created manager anyway.

    class CustomUserManager(BaseUserManager):

    def create_user(self, email, birth_date, password=None, **extra_fields):
    if not email:
    raise ValueError("You didn't entered a valid email address!")
    email = self.normalize_email(email)
    user = self.model(email=email, **extra_fields)
    user.set_password(password)
    user.save(using=self._db)
    return user

    def create_superuser(self, email, password, **extra_fields):
    extra_fields.setdefault('is_stuff', True)
    extra_fields.setdefault('is_superuser', True)

    return self.create_user(email, password, **extra_fields)


    I have something like this and in the end when it creates a user it doesn't go through that create_user anyway.

    So my two questions:


    1. Why create something for a custom user if I don't end up using it anyway because the form inherits from UserCreationForm. So, for example, form.save() does not use my manager.


    2. At what point should this create_user be executed?

    Continue reading...

Compartilhe esta Página