Django草莓写一个突变,其中包括一个外国钥匙,该突变创建多个对象

发布于 2025-02-11 15:51:01 字数 1993 浏览 3 评论 0原文

我正在尝试使GraphQl突变使用草莓立即为两个Django模型创建输入。我检查了文档

我有以下Django模型:

class Address(models.Model):
    name = models.CharField()

class Person(models.Model):
    name = models.CharField()
    address = models.ForeignKey('Address', on_delete=models.CASCADE, blank=False, null=False)

使用type.py文件:

@strawberry.django.type(models.Address)
class Address:
    id: auto
    name:auto

@strawberry.django.input(models.Address)
class AddressInput:
    id: auto
    name:auto

@strawberry.django.type(models.Person)
class Person:
    id: auto
    name: auto
    address:'Address'

@strawberry.django.input(models.Person)
class Person:
    id: auto
    name: auto
    address:'AddressInput'

对于schema.py我有:

@strawberry.type
class Mutation:
    createAddress: Address = mutations.create(AddressInput)
    createPerson: Person =mutations.create(PersonInput)

schema = strawberry.Schema(mutation=Mutation)

我尝试了突变,但是我有一个错误:

mutation newPerson ($name: String!, $addressname:String!){
  createPerson(data: {name: $name, address: {name: $addressname}}) {
    id
    name
    address {
      id
      name
    }
  }
}

#Query Variables

{
"name": "Min",
"addressname": "jkkihh",
}

错误消息:

"message": "Field 'id' expected a number but got PersonInput(id=<strawberry.unset._Unset object at 0x00000194FB945C90>, addressname='jkkihh', description=<strawberry.unset._Unset object at 0x00000194FB945C90>, notes=<strawberry.unset._Unset object at 0x00000194FB945C90>)."

这类似于这个问题我之前曾要求使用 graphene 。通过制作新对象类型存储并在类中写突变功能以进行突变来解决它。我还尝试进行两个突变,但是在创建外国钥匙地址的ID时,我遇到了问题。

I am trying to have a GraphQL mutation create the inputs for two Django models at once using strawberry. I checked the documentation here and there weren't any examples of how to do this.

I have the following Django model:

class Address(models.Model):
    name = models.CharField()

class Person(models.Model):
    name = models.CharField()
    address = models.ForeignKey('Address', on_delete=models.CASCADE, blank=False, null=False)

With the type.py file:

@strawberry.django.type(models.Address)
class Address:
    id: auto
    name:auto

@strawberry.django.input(models.Address)
class AddressInput:
    id: auto
    name:auto

@strawberry.django.type(models.Person)
class Person:
    id: auto
    name: auto
    address:'Address'

@strawberry.django.input(models.Person)
class Person:
    id: auto
    name: auto
    address:'AddressInput'

For the schema.py I have:

@strawberry.type
class Mutation:
    createAddress: Address = mutations.create(AddressInput)
    createPerson: Person =mutations.create(PersonInput)

schema = strawberry.Schema(mutation=Mutation)

I tried the Mutation, but I got an error:

mutation newPerson ($name: String!, $addressname:String!){
  createPerson(data: {name: $name, address: {name: $addressname}}) {
    id
    name
    address {
      id
      name
    }
  }
}

#Query Variables

{
"name": "Min",
"addressname": "jkkihh",
}

Error message:

"message": "Field 'id' expected a number but got PersonInput(id=<strawberry.unset._Unset object at 0x00000194FB945C90>, addressname='jkkihh', description=<strawberry.unset._Unset object at 0x00000194FB945C90>, notes=<strawberry.unset._Unset object at 0x00000194FB945C90>)."

This is similar the this question I previously asked using Graphene. Where it was resolved by making an new object type to store and wrote a mutate function inside a class for mutation. I also tried doing two mutations, but I had issues getting the id for the foreign key address when it was created.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北陌 2025-02-18 15:51:01

我认为 strawberry_django 在这里有一个错误,尽管您希望使用 Strawberry_django_plus 包装,
因为它可以更好地维护。希望它将很快将其合并到官方的草莓上。

在您的情况下,将是:

from strawberry_django_plus import gql
from strawberry import auto

@gql.django.type(models.Person)
class Person:
    id: auto
    name: auto
    address:'Address'

@gql.django.input(models.Person)
class Person:
    id: auto
    name: auto
    address:'AddressInput'

@gql.type
class Mutation:
    createAddress: Address = gql.django.create_mutation(AddressInput)
    createPerson: Person = gql.django.create_mutation(PersonInput)

schema = strawberry.Schema(mutation=Mutation)

I think strawberry_django has a bug here, though you should prefer using the strawberry_django_plus package,
because it is better maintained. Hopefully, it will be merged to the official strawberry soon.

In your case, it would be:

from strawberry_django_plus import gql
from strawberry import auto

@gql.django.type(models.Person)
class Person:
    id: auto
    name: auto
    address:'Address'

@gql.django.input(models.Person)
class Person:
    id: auto
    name: auto
    address:'AddressInput'

@gql.type
class Mutation:
    createAddress: Address = gql.django.create_mutation(AddressInput)
    createPerson: Person = gql.django.create_mutation(PersonInput)

schema = strawberry.Schema(mutation=Mutation)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文