自定义用户模型无效的django-allauth

时间:2015-12-08 13:06:16

标签: python django django-allauth

要使用我的自定义用户模型(消费AbstractBaseUser),我在settings.py中放了一些代码,但它无法正常工作.. 我根据需要使用电子邮件和用户名,并使用电子邮件作为登录方法。 请任何人帮助我T.T

python: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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

对不起。错误拼写错误..我在User类中将“object”更改为“objects”..