Graphql 试图创建 get_user_model 的对象:__init__() 得到了一个意外的关键字参数“请求”
Posted
技术标签:
【中文标题】Graphql 试图创建 get_user_model 的对象:__init__() 得到了一个意外的关键字参数“请求”【英文标题】:Graphql trying to create an object of get_user_model : __init__() got an unexpected keyword argument 'request' 【发布时间】:2020-05-25 15:13:10 【问题描述】:我最近开始学习 GraphQL。我为自定义模型创建了一个 Mutation,它工作正常。但是当我尝试对 Django 内置用户模型做同样的事情时。我收到一个错误。无法解决它。 我正在尝试通过 GraphQL 创建 get_user_model 对象来创建新用户。
class CreateUser(graphene.Mutation):
user = graphene.Field(UserType)
class Arguments:
username = graphene.String(required=True)
password = graphene.String(required=True)
email = graphene.String(request=True)
def mutate(self,info,username,password,email):
user = get_user_model(username=username,email=email)
user.set_password(password)
user.save()
return CreateUser(user=user)
class Mutation(graphene.ObjectType):
create_user = CreateUser.Field()
出现上述错误。不知道有什么问题。请任何人解释。
回溯错误:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/graphql/
Django Version: 3.0.3
Python Version: 3.6.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphene_django',
'links']
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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/django/views/generic/base.py", line 62, in view
self = cls(**initkwargs)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene_django/views.py", line 79, in __init__
schema = graphene_settings.SCHEMA
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene_django/settings.py", line 117, in __getattr__
val = perform_import(val, attr)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene_django/settings.py", line 56, in perform_import
return import_from_string(val, setting_name)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene_django/settings.py", line 70, in import_from_string
module = importlib.import_module(module_path)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
<source code not available>
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
<source code not available>
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
<source code not available>
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
<source code not available>
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
<source code not available>
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
<source code not available>
File "/Users/sunilhn/Documents/programming/django_fs/myDjangoProjects/graphqlhackernews/graphqlhackernews/schema.py", line 3, in <module>
import users.schema
File "/Users/sunilhn/Documents/programming/django_fs/myDjangoProjects/graphqlhackernews/users/schema.py", line 29, in <module>
class Mutation(graphene.ObjectType):
File "/Users/sunilhn/Documents/programming/django_fs/myDjangoProjects/graphqlhackernews/users/schema.py", line 30, in Mutation
create_user = CreateUser.Field()
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene/types/mutation.py", line 146, in Field
required=required,
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene/types/field.py", line 103, in __init__
self.args = to_arguments(args or OrderedDict(), extra_args)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene/types/argument.py", line 94, in to_arguments
arg = Argument.mounted(arg)
File "/Users/sunilhn/Documents/programming/Envs/graphenv/lib/python3.6/site-packages/graphene/types/mountedtype.py", line 19, in mounted
**unmounted.kwargs
Exception Type: TypeError at /graphql/
Exception Value: __init__() got an unexpected keyword argument 'request'
【问题讨论】:
您好 Sunil 我看到您正在创建“创建用户”突变,您可以尝试使用 django-graphql-auth。它确实提供了使用 graphql 的用户注册以及更多功能。 @pedroben 感谢您提供的信息。我会调查的。 【参考方案1】:解决了这个问题。定义电子邮件参数时有错字,而不是要求它被键入为请求。它现在正在工作。
【讨论】:
以上是关于Graphql 试图创建 get_user_model 的对象:__init__() 得到了一个意外的关键字参数“请求”的主要内容,如果未能解决你的问题,请参考以下文章
Apollo Client/GraphQL 实时更新 UI [重复]