authentication_app_react_django_rest
authentication_app_react_django_rest copied to clipboard
Custom class `AppUser` breaks command `./manage.py createsuperuser`, users cannot log into `admin`
Steps to reproduce:
./manage.py createsuperuser
email: [email protected]
username: joe
password: abc321
password: abc321
When trying to log in:
Solution:
- Add two Boolean fields to the
AppUser
:
class AppUser(AbstractBaseUser, PermissionsMixin):
user_id = models.AutoField(primary_key=True)
email = models.EmailField(max_length=50, unique=True)
username = models.CharField(max_length=50)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
objects = AppUserManager()
def __str__(self) -> str:
return self.username
- Fix
create_superuser()
method:
class AppUserManager(BaseUserManager):
def create_superuser(self, email, password=None, **extra_fields):
if not email:
raise ValueError('An email is required')
if not password:
raise ValueError('A password is required')
user = self.create_user(email, password)
user.is_staff = True
user.is_superuser = True
user.save()
return user