Unfortunately it seems I still can't get it to work. I tried creating users as I intend to do (no password) as well as with password - neither of them get to log in. The error is "No matching staff users for email address '[email protected]'
from django.db import models
from django.contrib.auth.models import (
AbstractBaseUser,
PermissionsMixin,
)
from core.managers import UserManager
class User(AbstractBaseUser, PermissionsMixin):
"""Custom user model that supports using login with
email instead of username"""
username = models.CharField(default="", max_length=255)
email = models.EmailField(unique=True, max_length=255)
archived = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
objects = UserManager()
EMAIL_FIELD = "email"
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username"]
class Meta:
verbose_name = "user"
verbose_name_plural = "users"
def __str__(self):
return f"{self.email} - {self.username}"
from django.contrib.auth.models import BaseUserManager
class UserManager(BaseUserManager):
def create_user(self, email, username, password=None, **kwargs):
"""To create user using custom fields"""
if not email:
raise ValueError("Invalid Email!!")
user = self.model(
email=self.normalize_email(email), username=username, **kwargs
)
if password is None:
user.set_unusable_password()
else:
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
"""To create superuser using custom fields"""
user = self.create_user(email=email, username=username, password=password)
user.is_staff = True
user.is_superuser = True
user.is_active = True
user.save(using=self._db)
return user
ADMIN_OAUTH_PATTERNS = [
(r"^.*@my\-domain\.com$", lambda match: match[0]),
(r"@my\-domain\.com$", "[email protected]"),
]