-
Notifications
You must be signed in to change notification settings - Fork 20
/
models.py
43 lines (28 loc) · 1.27 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractUser
from django.db import models
# Create your models here.
class CustomUserManager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser has to have is_staff being True")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser has to have is_superuser being True")
return self.create_user(email=email, password=password, **extra_fields)
class User(AbstractUser):
email = models.CharField(max_length=80, unique=True)
username = models.CharField(max_length=45)
date_of_birth = models.DateField(null=True)
objects = CustomUserManager()
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username"]
def __str__(self):
return self.username