繁体   English   中英

带有自定义用户模型的 django-allauth 不起作用

[英]django-allauth with Custom User Model not working

为了使用我的自定义用户模型(扩展 AbstractBaseUser),我在 settings.py 中添加了一些代码,但它不起作用..我根据需要使用电子邮件和用户名,并且电子邮件用作登录方法。 请任何人帮助我TT

蟒蛇:3.4/django:1.8.7/django-allauth:0.24.1

错误信息

 Environment:


Request Method: GET
Request URL: http://localhost:8000/accounts/google/login/callback/?state=6p1TrVVLFiJE&code=4/gXZLKhAK7mFc2zYwMQ6Z6m9SpOzXj8UXn6ESxz7gIhg


Django Version: 1.8.7
Python Version: 3.4.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.google',
 'main']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/socialaccount/providers/oauth2/views.py" in view
  55.             return self.dispatch(request, *args, **kwargs)
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/socialaccount/providers/oauth2/views.py" in dispatch
  125.             return complete_social_login(request, login)
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/socialaccount/helpers.py" in complete_social_login
  142.         return _complete_social_login(request, sociallogin)
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/socialaccount/helpers.py" in _complete_social_login
  158.         ret = _process_signup(request, sociallogin)
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/socialaccount/helpers.py" in _process_signup
  23.                                                        sociallogin)
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/socialaccount/adapter.py" in is_auto_signup_allowed
  136.                     if email_address_exists(email):
File "/Users/nuko/.virtualenvs/I_choose_you_pikachu/lib/python3.4/site-packages/allauth/utils.py" in email_address_exists
  104.             users = get_user_model().objects

Exception Type: AttributeError at /accounts/google/login/callback/
Exception Value: type object 'User' has no attribute 'objects'

在 models.py 中:

class User(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(max_length=250, unique=True)
    username = models.CharField(max_length=250, null=True)

    created_date = models.DateTimeField(auto_now_add=True, null=True)

    is_active = models.NullBooleanField(default=True, null=True)
    is_staff = models.NullBooleanField(default=False, null=True)

    USERNAME_FIELD = 'email'

    REQUIRED_FIELDS = ['username']

    object = UserManager()

    def get_full_name(self):
        return self.email

    def get_short_name(self):
        return self.email

    def __str__(self):
        return '{} ( {} )'.format(self.email, self.username)

    class Meta:
        swappable = 'AUTH_USER_MODEL'

在 settings.py 中:

# django-allauth settings

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

SITE_ID = 1
SOCIALACCOUNT_QUERY_EMAIL = True

SOCIALACCOUNT_PROVIDERS = {

    'facebook':
         {'METHOD': 'oauth2',
          'SCOPE': ['email'],
          'FIELDS': [
              'email',
              'name',
              'first_name',
              'last_name',
              'verified',
              'updated_time'],
          'VERIFIED_EMAIL': False,
          'VERSION': 'v2.4'},

    'google':
        {'SCOPE': ['profile', 'email', 'https://www.googleapis.com/auth/userinfo.email'],
             'AUTH_PARAMS': {
                 'access_type': 'online'
            }
        }
}

ACCOUNT_AUTHENTICATION_METHOD = "email"

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True

因此,如果您的自定义用户模型没有 username 字段,则需要将ACCOUNT_USER_MODEL_USERNAME_FIELD设置为None 这将禁用 allauth 中与用户名相关的功能。 请记住还将ACCOUNT_USERNAME_REQUIRED设置为False

有关更多信息,请参阅文档: https : //django-allauth.readthedocs.io/en/latest/advanced.html

对不起。 有一个拼写错误。我在用户类中将“对象”更改为“对象”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM