Graphql 试图创建 get_user_model 的对象:__init__() 得到了一个意外的关键字参数 'request'
Graphql trying to create an object of get_user_model : __init__() got an unexpected keyword argument 'request'
我最近开始学习 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'
问题已解决。定义电子邮件参数时有错别字,而不是要求将其键入为请求。现在可以使用了。
我最近开始学习 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'
问题已解决。定义电子邮件参数时有错别字,而不是要求将其键入为请求。现在可以使用了。