我已经开始学习GraphQL了一个星期,
而我最大的挑战是处理邮政数据。
我读过,这全都与突变有关,但是我不正确。
我只是想将Survivor(我的模型)对象发布到我的数据库中
这是代码:
这是应用程序的架构
import graphene
from graphene import relay, ObjectType
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Survivor
class SurvivorNode(DjangoObjectType):
class Meta:
model = Survivor
filter_fields = ['name']
interfaces = (relay.Node, )
class SurvivorInput(graphene.InputObjectType):
name = graphene.String(required=True)
age = graphene.Int(required=True)
class AddSurvival(graphene.Mutation):
class Arguments:
survivor_data = SurvivorInput()
survivor = graphene.Field(SurvivorNode)
@staticmethod
def mutate(root,info,survivor_data):
survivor=Survivor(
name = survivor_data.name,
age = survivor_data.age
)
return AddSurvival(survivor = survivor)
class Query(object):
all_survivors = DjangoFilterConnectionField(SurvivorNode)
survivor = relay.Node.Field(SurvivorNode)
请问我怎么了?
那我该怎么办?
答案 0 :(得分:1)
在mutate方法中,您需要更改
survivor=Survivor(
name = survivor_data.name,
age = survivor_data.age
)
对此
survivor=Survivor.objects.create(
name = survivor_data.name,
age = survivor_data.age
)
因为您要创建Survivor类型的新对象。