from django.utils.translation import gettext_lazy as _ from django.contrib.auth.models import BaseUserManager class CustomAccountManager(BaseUserManager): def create_superuser(self, email, username, first_name, password, **other_fields): """ Create a superuser with the given email, username, first_name, and password. """ other_fields.setdefault('is_staff', True) other_fields.setdefault('is_superuser', True) other_fields.setdefault('is_active', True) if other_fields.get('is_staff') is not True: raise ValueError('Superuser must be assigned to is_staff=True.') if other_fields.get('is_superuser') is not True: raise ValueError('Superuser must be assigned to is_superuser=True.') return self.create_user(email, username, first_name, password, **other_fields) def create_user(self, email, username, first_name, password, **other_fields): """ Create a user with the given email, username, first_name, and password. """ if not email: raise ValueError(_('You must provide an email address')) email = self.normalize_email(email) user = self.model(email=email, username=username, first_name=first_name, **other_fields) user.set_password(password) user.save() return user