Авторизация Google OAuth 2 - пользовательская модель User. Ошибка: У пользователя нет поля с именем 'username'
Яm trying to make the authorization via Google. I use a custom user model with an email as a username field. Once I login, I choose the email I want to login with. Then I
получил желтый экран с ошибкой "FieldDoesNotExist at /accounts/google/login/callback/
User has no field named 'username'". Если кто-нибудь может мне помочь, я буду очень признателен.
Мои модели:
# users/models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(db_index=True, unique=True, max_length=254)
first_name = models.CharField(max_length=30, help_text="Ваше имя")
last_name = models.CharField(blank=True, null=True, max_length=30, help_text="Ваша фамилия")
mobile = models.CharField(max_length=30, help_text='Ваш телефон')
status = models.CharField(blank=True, null=True, max_length=3, choices=TYPE_OF_CLIENT, default=POTENTIAL)
is_staff = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
Мои настройки:
INSTALLED_APPS = [
...
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
]
SITE_ID = 1
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = None
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
]
SOCIALACCOUNT_PROVIDERS = {
'google': {
'SCOPE': [
'profile',
'email',
],
'AUTH_PARAMS': {
'access_type': 'online',
}
}
}
URL PATTERNS
urlpatterns = [
...
path('accounts/', include('allauth.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Мои настройки URI Google введите описание изображения здесь
Ошибка введите описание изображения здесь
Я нашел решение
SITE_ID = 1
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_FORMS = {'signup': 'users.forms.CreationForm'}