Make sure `createuser
and
createsuperuser
` work
This commit is contained in:
parent
f7f262ec75
commit
4dd7d8628d
1 changed files with 26 additions and 1 deletions
|
@ -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.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
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):
|
class User(AbstractBaseUser):
|
||||||
"""The Django model provides a password and last_login field."""
|
"""The Django model provides a password and last_login field."""
|
||||||
|
|
||||||
|
objects = UserManager()
|
||||||
|
|
||||||
email = models.EmailField(unique=True, verbose_name=_("Email address"))
|
email = models.EmailField(unique=True, verbose_name=_("Email address"))
|
||||||
first_name = models.CharField(
|
first_name = models.CharField(
|
||||||
max_length=30, blank=True, verbose_name=_("First name")
|
max_length=30, blank=True, verbose_name=_("First name")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue