Skip to content

Commit

Permalink
Add custom UserManager with create_user without username (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestofgonzalez committed Jun 6, 2023
1 parent af91676 commit 0063e44
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import stripe
from django.conf import settings
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db import models
from django.utils.functional import cached_property
from phonenumber_field.modelfields import PhoneNumberField
from {{cookiecutter.project_slug}}.utils import default_uuid


class UserManager(BaseUserManager):
def create_user(self, email, password=None, name=None, **extra_fields):
if not email:
raise ValueError('Enter an email address')
if not name:
raise ValueError('Enter a name')
email = self.normalize_email(email)
user = self.model(email=email, name=name, **extra_fields)
user.set_password(password)
user.save()
return user

def create_superuser(self, email, name, password):
user = self.create_user(email, name=name, password=password)
user.is_superuser = True
user.is_staff = True
user.save()
return user


class User(AbstractUser):
uuid = models.CharField(
default=default_uuid, editable=False, unique=True, max_length=255
Expand All @@ -28,6 +48,8 @@ class User(AbstractUser):
null=True, blank=False, max_length=255,
)

objects = UserManager()

USERNAME_FIELD = "email"
REQUIRED_FIELDS = [
"name",
Expand Down

0 comments on commit 0063e44

Please sign in to comment.