/auth/convert-token 需要 create_user 中的用户名

/auth/convert-token requires username in create_user

我有自定义用户模型,我的用户管理器如下所示

class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)

class User(AbstractUser):
    #USERNAME_FIELD = 'email'
    #REQUIRED_FIELDS = ['email']
    username = None
    email = models.EmailField(_('email address'), unique=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    def __str__(self):
        return self.email
    pass

在settings.py

ACCOUNT_AUTHENTICATION_METHOD = 'email' 
ACCOUNT_USER_MODEL_USERNAME_FIELD = None 
ACCOUNT_EMAIL_REQUIRED = True 
ACCOUNT_USERNAME_REQUIRED = False 
ACCOUNT_EMAIL_VERIFICATION = 'none'
AUTH_USER_MODEL = 'defapp.User'

不需要用户名,而是使用电子邮件。

现在我尝试将 Django rest framework 设置为使用 oauth(google 登录)

我的第一个测试是post这里注册用户的token

http://localhost:8008/auth/convert-token

但是出现了错误。

TypeError at /auth/convert-token
create_user() missing 1 required positional argument: 'username'

我的 create_user 不需要用户名,但它需要。

这是我的堆栈跟踪。

感谢您的帮助。

Environment:


Request Method: POST
Request URL: http://localhost:8008/auth/convert-token

Django Version: 3.1.7
Python Version: 3.7.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'defapp.apps.DefappConfig',
 'rest_framework',
 'rest_framework.authtoken',
 'rest_auth',
 'rest_auth.registration',
 'django_filters',
 'django_extensions',
 'corsheaders',
 'crispy_forms',
 'register.apps.RegisterConfig',
 'oauth2_provider',
 'social_django',
 'drf_social_oauth2',
 'django.contrib.sites',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.google']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/drf_social_oauth2/views.py", line 37, in dispatch
    return super(CsrfExemptMixin, self).dispatch(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/drf_social_oauth2/views.py", line 93, in post
    url, headers, body, status = self.create_token_response(request._request)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/oauth2_provider/views/mixins.py", line 124, in create_token_response
    return core.create_token_response(request)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/drf_social_oauth2/oauth2_backends.py", line 30, in create_token_response
    return super(KeepRequestCore, self).create_token_response(request)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/oauth2_provider/oauth2_backends.py", line 156, in create_token_response
    uri, http_method, body, headers, extra_credentials
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/endpoints/base.py", line 116, in wrapper
    return f(endpoint, uri, *args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/drf_social_oauth2/oauth2_endpoints.py", line 92, in create_token_response
    request, self.default_token_type
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/oauthlib/oauth2/rfc6749/grant_types/refresh_token.py", line 60, in create_token_response
    self.validate_token_request(request)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/drf_social_oauth2/oauth2_grants.py", line 100, in validate_token_request
    user = backend.do_auth(access_token=request.token)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/utils.py", line 248, in wrapper
    return func(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/backends/oauth.py", line 403, in do_auth
    return self.strategy.authenticate(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_django/strategy.py", line 107, in authenticate
    return authenticate(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate
    user = backend.authenticate(request, **credentials)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/backends/base.py", line 80, in authenticate
    return self.pipeline(pipeline, *args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/backends/base.py", line 83, in pipeline
    out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/backends/base.py", line 113, in run_pipeline
    result = func(*args, **out) or {}
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/pipeline/user.py", line 79, in create_user
    'user': strategy.create_user(**fields)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_core/strategy.py", line 54, in create_user
    return self.storage.user.create_user(*args, **kwargs)
  File "/Users/whitebear/anaconda3/envs/myapp/lib/python3.7/site-packages/social_django/storage.py", line 80, in create_user
    user = cls.user_model().objects.create_user(*args, **kwargs)

Exception Type: TypeError at /auth/convert-token
Exception Value: create_user() missing 1 required positional argument: 'username'

尝试将 UserManager 设置为自定义 User 模型的管理员:

class User(AbstractUser):
    objects = UserManager()