GraphQL突变创建外键地址和人

时间:2019-10-10 10:36:42

标签: django graphql graphene-django

我看了很多关于外键突变的文章,没有任何帮助。

我需要创建一个具有Django模型Person的人,该人的地址是Address模型的外键。一个人需要一个地址!

我在Django中的模型是:

from django.db import models


class Address(models.Model):
    class Meta:
        verbose_name_plural = "Addresses"

    number = models.CharField(max_length=4)
    street = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    postcode = models.CharField(max_length=7)


class Person(models.Model):
    class Meta:
        verbose_name_plural = "Persons"

    avatar = models.ImageField(upload_to='images/avatars/')
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    address = models.ForeignKey(Address, on_delete=models.CASCADE)
    personal_image = models.ImageField(upload_to='images/personal_images/')

    def get_fullname(self):
        return self.first_name + ' ' + self.last_name

我在架构中的变异是:

class AddressInput(graphene.InputObjectType):
    number = graphene.String(required=True)
    street = graphene.String(required=True)
    city = graphene.String(required=True)
    postcode = graphene.String(required=True)


class CreatePerson(graphene.Mutation):
    class Arguments:
        avtr = Upload(required=False)
        fname = graphene.String(required=True)
        lname = graphene.String(required=True)
        addr = graphene.Argument(AddressInput, required=False)
        pimage = Upload(required=False)


    person = graphene.Field(PersonType)

    def mutate(self, info, avtr, fname, lname, addr, pimage):
        person = Person(
            avatar=avtr,
            firstName=fname,
            lastName=lname,
            address=addr,
            personalImage=pimage
        )
        return CreatePerson(person=person)

class Mutations(graphene.ObjectType):
    create_person = CreatePerson.Field()

我正在用GraphiQL进行测试,我的突变atm看起来像这样:

mutation createPerson {
  createPerson(avtr: None, fname: "Bob", lname: "Diamond", addr: {number: "10", street: "Hormond Avenue", city: "Shrewsbury", postcode: "N7 9RF"}, pimage: None) {
    person {
      firstName
      lastName
    }
  }
}

我得到的输出是:

{
  "errors": [
    {
      "message": "Cannot assign \"{'number': '10', 'street': 'Hormond Avenue', 'city': 'Shrewsbury', 'postcode': 'N7 9RF'}\": \"Person.address\" must be a \"Address\" instance.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "createPerson"
      ]
    }
  ],
  "data": {
    "createPerson": null
  }
}

1 个答案:

答案 0 :(得分:0)

一个人应该只是一个地址,还是一个人有多个地址?


我认为您需要创建GraphQL类型,而不仅仅是突变。例如:

class PersonNode(DjangoObjectType):
    address = graphene.Field(AddressObjectType)

    class Meta:
        model = Person

    def resolve_address(self, info):
        address = self.person.address
        return AddressObjectType(
          id=address.id,
          street=address.street,
          zip=address.zip
        )

class AddressNode(DjangoObjectType):
    id = graphene.ID()
    street = graphene.String()
    zip = graphene.Int()