gpt4 book ai didi

graphene-django - 有没有办法优雅地使用 django 模型作为输入对象类型?

转载 作者:行者123 更新时间:2023-12-05 06:04:24 24 4
gpt4 key购买 nike

说我有,

class PersonNode(DjangoObjectType):
class Meta:
model = Person
fields = ('first_name', 'last_name', 'age',
'sex', 'alive', 'unique_identifier',)
filter_fields = ('first_name', 'last_name', 'age',
'sex', 'alive', 'unique_identifier',)
interfaces = (graphene.relay.Node,)

class PersonNodeInput(graphene.InputObjectType):
first_name = graphene.String()
last_name = graphene.String()
# rest of person model fields

class Valid(graphene.ObjectType):
ok = graphene.Boolean()

class Query(graphene.ObjectType):
validate_person = graphene.Field(Valid, args={
"data": PersonNodeInput()})
person = graphene.relay.Node.Field(PersonNode)
all_people = DjangoFilterConnectionField(PersonNode)

def resolve_validate_person(root, info, data):
print("resolve validate person")
return Valid(ok=True)

是否可以避免写出 PersonNodeInput?如果您可以将“DjangoInputObjectType”之类的子类化并在 Meta 属性中指定您想要的模型和字段,那就太好了。

最佳答案

我们一直在使用这个 Graphene Django CUD试图删除一些涉及定义突变的样板的库。

根据他们的示例调整您的案例看起来像

from graphene_django_cud.mutations import DjangoCreateMutation

class CreatePersonMutation(DjangoCreateMutation):
class Meta:
model = Person

class PersonMutations(graphene.ObjectType):
create_person = CreateUserMutation.Field()

schema = Schema(mutation=PersonMutations)

我不知道它与 Relay 节点的配合情况如何,因为我们没有使用 Relay。

关于graphene-django - 有没有办法优雅地使用 django 模型作为输入对象类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66286436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com