[英]how to solve error “'NoneType' object has no attribute 'day'” in django application
[英]How to solve 'NoneType' object has no attribute 'fields' in Graphene-django
有这个突变
class AddStudentMutation(graphene.Mutation):
class Arguments:
input = StudentInputType()
student = graphene.Field(StudentType)
@classmethod
@staff_member_required
def mutate(cls, root, info, input):
try:
_student = Student.objects.create(**input)
except IntegrityError:
raise Exception("Student already created")
return AddStudentMutation(student=_student)
在graphiql
中执行上述突变之前,我添加了请求 header "Authorization": "JWT <token>"
以便用户获得授权。 但我收到错误graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'fields'
。 当我删除 header 时,错误不会发生。当我将它包含在查询请求中时,它也能正常工作。 难道我做错了什么? 我也需要授权才能发生突变。
我跟踪了 Traceback,它指向文件.../site-packages\graphql_jwt\middleware.py
。 看来这是 function allow_any()
line 18 field = info.schema.get_type(operation_name).fields.get(info.field_name)
中的 package 中的错误。 对此我很陌生,我需要帮助。
我正在使用graphene-django==2.15.0
2.15.0 和django-graphql-jwt==0.3.4
django-graphql-jwt
附带的allow_any
function 期望以某种方式与查询而不是突变一起使用。 因此,您可以通过添加本机try/except
块来覆盖allow_any
function:
def allow_any(info, **kwargs):
try:
operation_name = get_operation_name(info.operation.operation).title()
operation_type = info.schema.get_type(operation_name)
if hasattr(operation_type, 'fields'):
field = operation_type.fields.get(info.field_name)
if field is None:
return False
else:
return False
graphene_type = getattr(field.type, "graphene_type", None)
return graphene_type is not None and issubclass(
graphene_type, tuple(jwt_settings.JWT_ALLOW_ANY_CLASSES)
)
except Exception as e:
return False
在您的settings.py
,您必须添加覆盖的allow_any
function 的路径:
GRAPHQL_JWT = {
'JWT_ALLOW_ANY_HANDLER': 'path.to.middleware.allow_any'
}
我希望这可以解决您的问题,因为它与我一起工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.