Created
December 11, 2023 10:22
-
-
Save ManotLuijiu/c83d67d053c565b7b600bb53366034bc to your computer and use it in GitHub Desktop.
users -> models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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