# users/models.py
from django.db import models
from django.contrib.auth.models import (
BaseUserManager, AbstractBaseUser
)
class UserManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password=None):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
email,
password=password,
date_of_birth=date_of_birth,
)
user.is_admin = True
user.save(using=self._db)
return user
class User(AbstractBaseUser):
email = models.EmailField( # ์ด๋ฉ์ผ๋ก ๋ค์ด์๊ณ
verbose_name='email address',
max_length=255,
unique=True,
)
date_of_birth = models.DateField() # ์๋
์์ผ
is_active = models.BooleanField(default=True) # ํ๋ ์ค์ธ์ง
is_admin = models.BooleanField(default=False) # ์ด๋๋ฏผ์ธ์ง
objects = UserManager() # ์ ์ ๋งค๋์ ์ฐ๊ฒฐ
USERNAME_FIELD = 'email' # ์ด๋ฉ์ผ๋ก ๋ก๊ทธ์ธ
REQUIRED_FIELDS = ['date_of_birth'] # ์๋
์์ผ ๊ผญ ํ์๋ก ๋ฃ์ด๋ฌ๋ผ
def __str__(self):
return self.email
def has_perm(self, perm, obj=None): # ํผ๋ฏธ์
์ด ์๋์ง
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self): # ์คํํ์ธ์ง?
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
# settings.py
...
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
AUTH_USER_MODEL = 'users.user' # '์ฑ.๋ชจ๋ธ'
db.sqlite3๋ฅผ ์ญ์ ํ๊ณ , UserModel์ Custom Model๋ก ๋ฐ๊พธ๊ณ migrate๋ฅผ ์งํํ์๋ค.
user๋ชจ๋ธ์ Custom user๋ก ๋ง๋ค๊ธฐ ํ๋ ์ด์
Changing to a custom user model mid-project
Changingย
[AUTH_USER_MODEL](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-AUTH_USER_MODEL)
ย after youโve created database tables is significantly more difficult since it affects foreign keys and many-to-many relationships, for example.This change canโt be done automatically and requires manually fixing your schema, moving your data from the old user table, and possibly manually reapplying some migrations. Seeย #25313ย for an outline of the steps.
Due to limitations of Djangoโs dynamic dependency feature for swappable models, the model referenced byย
[AUTH_USER_MODEL](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-AUTH_USER_MODEL)
ย must be created in the first migration of its app (usually calledย0001_initial
); otherwise, youโll have dependency issues.In addition, you may run into aย
CircularDependencyError
ย when running your migrations as Django wonโt be able to automatically break the dependency loop due to the dynamic dependency. If you see this error, you should break the loop by moving the models depended on by your user model into a second migration. (You can try making two normal models that have aยForeignKey
ย to each other and seeing howยmakemigrations
ย resolves that circular dependency if you want to see how itโs usually done.)ํ๋ก์ ํธ ์ค๊ฐ์ ์ปค์คํ ์ฌ์ฉ์ ๋ชจ๋ธ๋ก ๋ณ๊ฒฝํ๊ธฐ
์๋ฅผ ๋ค์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์ด๋ธ์ ์์ฑํ ํ ๋ณ๊ฒฝย
[AUTH_USER_MODEL](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-AUTH_USER_MODEL)
ํ๋ ๊ฒ์ ์ธ๋ ํค ๋ฐ ๋ค๋๋ค ๊ด๊ณ์ ์ํฅ์ ๋ฏธ์น๊ธฐ ๋๋ฌธ์ ํจ์ฌ ๋ ์ด๋ ต์ต๋๋ค.์ด ๋ณ๊ฒฝ์ ์๋์ผ๋ก ์ํํ ์ ์์ผ๋ฉฐ ์๋์ผ๋ก ์คํค๋ง๋ฅผ ์์ ํ๊ณ ์ด์ ์ฌ์ฉ์ ํ ์ด๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ์ด๋ํ๊ณ ์ผ๋ถ ๋ง์ด๊ทธ๋ ์ด์ ์ ์๋์ผ๋ก ๋ค์ ์ ์ฉํด์ผ ํฉ๋๋ค.ย ๋จ๊ณ์ ๋ํ ๊ฐ์๋ย #25313ย ์ ์ฐธ์กฐ ํ์ญ์์ค.
๊ต์ฒด ๊ฐ๋ฅํ ๋ชจ๋ธ์ ๋ํ Django์ ๋์ ์ข ์์ฑ ๊ธฐ๋ฅ์ ์ ํ์ผ๋ก ์ธํด ์์ ์ฐธ์กฐํ๋ ๋ชจ๋ธย
[AUTH_USER_MODEL](https://docs.djangoproject.com/en/4.1/ref/settings/#std-setting-AUTH_USER_MODEL)
์ ์ฑ์ ์ฒซ ๋ฒ์งธ ๋ง์ด๊ทธ๋ ์ด์ (๋ณดํต ์ด๋ผ๊ณ ํจย0001_initial
)์์ ์์ฑ๋์ด์ผ ํฉ๋๋ค.ย ๊ทธ๋ ์ง ์์ผ๋ฉด ์ข ์์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.๋ํย
CircularDependencyError
Django๊ฐ ๋์ ์ข ์์ฑ์ผ๋ก ์ธํด ์ข ์์ฑ ๋ฃจํ๋ฅผ ์๋์ผ๋ก ์ค๋จํ ์ ์๊ธฐ ๋๋ฌธ์ ๋ง์ด๊ทธ๋ ์ด์ ์ ์คํํ ๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ต๋๋ค.ย ์ด ์ค๋ฅ๊ฐ ํ์๋๋ฉด ์ฌ์ฉ์ ๋ชจ๋ธ์ ์ข ์๋ ๋ชจ๋ธ์ ๋ ๋ฒ์งธ ๋ง์ด๊ทธ๋ ์ด์ ์ผ๋ก ์ด๋ํ์ฌ ๋ฃจํ๋ฅผ ๋์ด์ผ ํฉ๋๋ค.ยForeignKey
( ์๋กย ๊ฐ ์๋ ๋ ๊ฐ์ ์ผ๋ฐ ๋ชจ๋ธ์ ๋ง๋ค๊ณ ยmakemigrations
์ผ๋ฐ์ ์ผ๋ก ์ํ๋๋ ๋ฐฉ์์ ๋ณด๋ ค๋ฉด ์ํ ์ข ์์ฑ์ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ณผ ์ ์์ต๋๋ค.)
Django ๊ณต์๋ฌธ์์์ ์ฐพ์๋ณด๊ณ ์์ํ๋ ๊ฒ์ด ์ข์ ๋ฏํ๋ค.