Make sure `createuser and createsuperuser` work

This commit is contained in:
Tobias Kunze 2025-03-16 09:22:33 +01:00
parent f7f262ec75
commit 4dd7d8628d

View file

@ -1,11 +1,36 @@
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import models
from django.utils.translation import gettext_lazy as _
class UserManager(BaseUserManager):
"""
Use a separate model manager, as we do not have a username field.
"""
use_in_migrations = True
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError("Please provide an 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):
"""Create and save a SuperUser with the given email and password."""
extra_fields["is_staff"] = True
extra_fields["is_superuser"] = True
return self.create_user(email, password, **extra_fields)
class User(AbstractBaseUser):
"""The Django model provides a password and last_login field."""
objects = UserManager()
email = models.EmailField(unique=True, verbose_name=_("Email address"))
first_name = models.CharField(
max_length=30, blank=True, verbose_name=_("First name")