Django草莓写一个突变,其中包括一个外国钥匙,该突变创建多个对象
我正在尝试使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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 strawberry_django 在这里有一个错误,尽管您希望使用 Strawberry_django_plus 包装,
因为它可以更好地维护。希望它将很快将其合并到官方的草莓上。
在您的情况下,将是:
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: