3

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

Graphql 响应:

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

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

我的 schema.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 函数吗?(文档不清楚)

谢谢!

4

1 回答 1

1

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

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()
于 2020-06-19T00:16:43.060 回答