model.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from .managers import UserAccountManager
class UserAccount(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
max_length=255,
unique=True,
# null=False,
# blank=False,
verbose_name="email",
)
name = models.CharField(max_length=255)
is_active=models.BooleanField(
default=True,
)
is_staff=models.BooleanField(
default=False,
)
is_admin=models.BooleanField(
default=False,
)
created_at=models.DateTimeField(auto_now_add=True)
objects: UserAccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
return self.email
managers.py
'''
from django.contrib.auth.models import BaseUserManager
class UserAccountManager(BaseUserManager): def create_user(self, email, name, password=None): """Creates and saves a User with the given email, name and password.""" if not email: raise ValueError('Users must have an email address')
email=self.normalize_email(email)
email = email.lower()
user = self.model(
email=email,
name=name
)
user.set_password(password)
user.save(using=self._db)
return user
def create_admin(self, email, name, password=None):
"""
Creates and saves a superuser with the given email, name and password.
"""
user = self.create_user(email, name, password)
user.is_admin = True
user.is_staff = True
user.save(using=self._db)
return user
def create_superuser(self, email, name, password=None):
"""
Creates and saves a superuser with the given email, name and password.
"""
user = self.create_user(email, name, password)
user.is_staff = True
user.is_admin = True
user.is_superuser = True
user.save(using=self._db)
return user
'''
CodePudding user response:
You annotated the objects
, you did not set it to a UserAccountManager()
object, indeed:
class UserAccount(AbstractBaseUser, PermissionsMixin):
# …
#