突变 - 批量创建对象 [英] Mutations - batch creation of objects

查看:24
本文介绍了突变 - 批量创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用石墨烯一口气创造很多人.文档只提到了这样创建一个人的方法:

I want to use graphene to create many people in one go. The document only mention the way to create one person like this:

class CreatePerson(graphene.Mutation):
class Input:
    name = graphene.String()
    age = graphene.Int()

ok = graphene.Boolean()
person = graphene.Field(lambda: Person)

@staticmethod
def mutate(root, args, context, info):
    person = Person(name=args.get('name'), age=args.get('age'), mobile=args.get('mobile'))
    ok = True
    return CreatePerson(person=person, ok=ok)

有什么方法可以完成吗?

are there any ways to get it done?

推荐答案

我可以根据 Jan Hančič

在这种情况下使用一种叫做 graphene.InputObjectType 的类型

There is a type called graphene.InputObjectType to use in this case

解决办法是

class PersonInput(InputObjectType):
    name = graphene.String()
    age = graphene.Int()

class CreatePeople(graphene.Mutation):
    class Input:
       people = graphene.List(PersonInput)

    people = graphene.List(lambda: Person)

    @staticmethod
    def mutate(root, args, context, info):
        people = [Person.objects.create(name=person.name, age=person.age) for person in args.get('people')]
        return CreatePeople(people=people)

这篇关于突变 - 批量创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆