gpt4 book ai didi

django - 如何让 Django Graphene ModelForm Mutation 应用

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

我试图让这个突变在数据库中创建一个新记录。它返回代码 200,但没有对数据库进行任何更改,而且它返回 null。文档在这个问题上并不清楚。(ModelForm vs mutate function)

Graphql 响应:

{
"data": {
"addSubjectMark": {
"subjectMark": null,
"errors": []
}
}
}

根据 django-graphene 文档,我使用 DjangoModelForm 来处理数据库的输入。

我的架构.py:

class SubjectMarkType(DjangoObjectType):
id = graphene.ID(required=True)
class Meta:
model = SubjectMark

class AddSubjectMarkMutation(DjangoModelFormMutation):
subject_mark = graphene.Field(SubjectMarkType)
class Meta:
form_class = ReportForm

class Mutation(graphene.ObjectType):
add_subject_mark = AddSubjectMarkMutation.Field()
  1. 我需要在表单中添加保存方法吗?
  2. 我需要使用 mutate 函数吗?(文档不清楚)

谢谢!

最佳答案

ReportForm 应该默认使用 Django,无需修改。缺少的部分是解析 AddSubjectMarkMutation 类中的 subject_mark 属性。

class SubjectMarkType(DjangoObjectType):
id = graphene.ID(required=True)

class Meta:
model = SubjectMark


class AddSubjectMarkMutation(DjangoModelFormMutation):
subject_mark = graphene.Field(SubjectMarkType)

class Meta:
form_class = ReportForm # NB. make sure ReportForm is able to save in Django

# you need to resolve subject_mark to return the new object
def resolve_subject_mark(self, info, **kwargs):
return self.subjectMark


class Mutation(graphene.ObjectType):
add_subject_mark = AddSubjectMarkMutation.Field()

关于django - 如何让 Django Graphene ModelForm Mutation 应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62017526/

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