未捕获(承诺中)错误:GraphQL错误:不可哈希类型:'dict'

时间:2018-10-19 12:12:49

标签: python django graphql graphene-python

我正在尝试具有图像上传功能,但出现错误“未捕获(承诺)错误: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对象。

enter image description here

使用django-graphene时如何上传图像?

1 个答案:

答案 0 :(得分:0)

这是我用过的很好的包裹,很满意:graphene-file-upload

它处理多部分数据并将实际变量替换为传递的文件。