알다시피 .authenticate는 무조건 username이 필요로하다.
하지만 하단의 문서를 보면
authenticate(request, username=None, password=None, **kwargs)¶
Tries to authenticate username with password by calling User.check_password. If no username is provided, it tries to fetch a username from kwargs using the key CustomUser.USERNAME_FIELD. Returns an authenticated user or None.
request is an HttpRequest and may be None if it wasn’t provided to authenticate() (which passes it on to the backend).
(출처: Django 공식문서 - https://docs.djangoproject.com/en/4.0/ref/contrib/auth/)
CustomUser.USERNAME_FIELD를 사용하여 username을 다른걸로 바꿀수 있다.
이를 위해 모델에
class CustomUser(AbstractUser):
class Meta:
db_table = "custom_user"
USERNAME_FIELD = 'email'
를 추가하면 하단과 같은 에러가 나온다.(물론 makemigartes 했을때)
user.CustomUser: (auth.E002) The field named as the 'USERNAME_FIELD' for a custom user model must not be included in 'REQUIRED_FIELDS'.
HINT: Your username field is currently set to "email", you should remove "email" from your required fields definition.
이는
class CustomUser(AbstractUser):
class Meta:
db_table = "custom_user"
REQUIRED_FIELDS = ['username']
USERNAME_FIELD = 'email'
로 해결이 된다.
이유는 REQUIRED_FIELDS에 USERNAME_FIELD와 비밀번호는 있으면 안되고 이를 제거 해주면 된다. 제거 방법은 REQUIRED_FIELDS를 재설정을 해주는 것이다.
REQUIRED_FIELDS¶
A list of the field names that will be prompted for when creating a user via the createsuperuser management command. The user will be prompted to supply a value for each of these fields. It must include any field for which blank is False or undefined and may include additional fields you want prompted for when a user is created interactively. REQUIRED_FIELDS has no effect in other parts of Django, like creating a user in the admin.
For example, here is the partial definition for a user model that defines two required fields - a date of birth and height:
class MyUser(AbstractBaseUser):
...
date_of_birth = models.DateField()
height = models.FloatField()
...
REQUIRED_FIELDS = ['date_of_birth', 'height']
#Note
#REQUIRED_FIELDS must contain all required fields on your user model, but should not contain the USERNAME_FIELD or password as these fields will always be prompted for.
(출처: Django 공식문서 - https://docs.djangoproject.com/en/4.0/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD)
근데 그러면 또 error auth.E003이 나온다.
이는 email이 unique값이 아니여서 나오는 것인데
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
class Meta:
db_table = "custom_user"
email = models.EmailField(max_length=255, unique=True)
REQUIRED_FIELDS = ['username']
USERNAME_FIELD = 'email'
이렇게 재설정해주면 끝!
아래는 바뀌기 전후를 보여준다.
'Django > Django Vanila' 카테고리의 다른 글
22. Django annotate (0) | 2022.06.15 |
---|---|
21. Django Queryset 활용(select_related, prefetch_related) (0) | 2022.06.15 |
19. Django admin 편집 (0) | 2022.06.06 |
18. Django MySQL 연동(연결) (0) | 2022.06.06 |
17. Django 가비아에서 도메인 구입 (0) | 2022.05.30 |