我正在尝试具有图像上传功能,但出现错误“未捕获(承诺)错误:GraphQL错误:无法哈希的类型:'dict'”。我正在使用react-dropzone
上传图片。它给了我File对象,然后我将其传递给带有变量的变异函数。这就是我已经完成的
mutation.py
class UpdatePersonalProfile(graphene.Mutation):
class Arguments:
input = ProfileInput(description="These fields are required", required=True)
success = graphene.Boolean()
errors = graphene.List(graphene.String)
profile = graphene.Field(ProfileNode)
@staticmethod
def mutate(self, info, **args):
print ('info', args, info.context, info.context.FILES, info.context.FILES.get(args.get('input').get('avatar', None)))
is_authenticated = info.context.user.is_authenticated
profile = Profile.objects.get(user=CustomUser.objects.get(id=7))
profile.company_name = args.get('input').get('company_name', None)
profile.bio = args.get('input').get('bio', None)
profile.website = args.get('input').get('website', None)
profile.avatar = info.context.FILES.get(args.get('input').get('avatar', None))
profile.job_title = args.get('input').get('job_title', None)
profile.zip_code = args.get('input').get('zip_code', None)
profile.save()
return UpdatePersonalProfile(profile=profile, success=True, errors=None)
input.py
class Upload(graphene.types.Scalar):
class Meta:
description = '''Variables of this type must be set to null in
mutations. They will be replaced with a filename from a following
multipart part containing a binary file. See:
https://github.com/jaydenseric/graphql-multipart-request-spec'''
@staticmethod
def serialize(value):
return value
@staticmethod
def parse_literal(node):
return node
@staticmethod
def parse_value(value):
return value
class ProfileInput(graphene.InputObjectType):
full_name = graphene.String(description='Full Name')
user = graphene.String(description='User')
bio = graphene.String(description='No more than 1000 characters')
website = graphene.String()
avatar = Upload(description='Avatar')
job_title = graphene.String()
company_name = graphene.String()
zip_code = graphene.String()
这是我使用过的文件[0]的化身密钥的数据,这意味着整个File对象。
使用django-graphene时如何上传图像?