Skip to content

Instantly share code, notes, and snippets.

@ManotLuijiu
Created December 11, 2023 10:22
Show Gist options
  • Save ManotLuijiu/c83d67d053c565b7b600bb53366034bc to your computer and use it in GitHub Desktop.
Save ManotLuijiu/c83d67d053c565b7b600bb53366034bc to your computer and use it in GitHub Desktop.
users -> models.py
from django.db import models
from django.contrib.auth.models import (
BaseUserManager,
AbstractBaseUser,
PermissionsMixin,
)
class UserAccountManager(BaseUserManager):
def create_user(self, email, password=None, **kwargs):
if not email:
raise ValueError("โปรดใส่อีเมลด้วยค่ะ")
email = self.normalize_email(email)
email = email.lower()
user = self.model(email=email, **kwargs)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None, **kwargs):
user = self.create_user(
email,
password=password,
**kwargs
)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class UserAccount(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.EmailField(
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
objects = UserAccountManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["first_name", "last_name"]
def __str__(self):
return self.email
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment