회원가입과 로그인 일지(to be continued...)

노그리·2022년 4월 15일
0

🔨 Django

목록 보기
1/2

막힌 부분

1. 커스텀 User 모델 대체하기

​ ❌ 명세에 적혀있는 User모델 필드를 따로 정의해야 한다고 생각했다.

​ ⭕ 상속받는AbstractUser에 클래스 속성으로 이미 포함되어 있음!

​ ❌ User 모델 직접 참조함 : accounts/forms.py에서 model = User

​ ⭕ get_user_model() : User 모델을 참조할 때(forms.py빼고 다)

# 1. django.contrib.auth.get_user_model()
# 2. from django.contrib.auth import get_user_model

​ ⭕ AUTH_USER_MODEL : 외래키로 User 모델을 참조할 때 대체 방법(forms.py에서만)

# from django.conf import settings

2. UserCreationForm 사용하기

​ ❌ ArticleForm을 만드는 것처럼 UserForm을 정의하는 줄 알았다.

​ ⭕ 회원가입할 때는 UserCreationForm을 상속받아서 사용!!!

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm


class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = get_user_model()
        fields = UserCreationForm.Meta.fields
profile
자기소개가 싫어요

0개의 댓글