石墨烯-django与ManyToMany&直通表 [英] Graphene-django with ManyToMany & through-table
问题描述
我的应用具有以下几种通过模型的多对多关系:
My app has several many-to-many relationships with a through-model like so:
class Person(models.Model):
name = models.CharField()
class Group(models.Model):
name = models.CharField()
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField() # Extra info on the relationship
在没有成员资格中间类型(选项A)的graphql中表示此数据似乎很直观:
It would seem intuitive to represent this data in graphql without an intermediate type for Membership (option A):
{
"data": {
"persons": [
{
"id": "1",
"name": "Jack",
"groups": [
{
"id": 3, # From Group-model
"name": "Students", # From Group-model
"date_joined": "2019-01-01" # From Membership-model
},
...
]
}
]
}
}
vs.选项B:
{
"data": {
"persons": [
{
"id": "1",
"name": "Jack",
"memberships": [
{
"id": 9,
"date_joined": "2019-01-01"
"group": {
"id": 3,
"name": "Students"
}
},
...
]
}
]
}
}
我找不到有关如何使用(django-)石墨烯实现选项A的任何示例.如何做到这一点,是否支持开箱即用?
I could not find any examples on how to implement option A with (django-)graphene. How could it be done and is this supported to work out of the box?
两种方法的优缺点是什么?数据也需要经常进行突变,这会改变判决吗?
What are the pros and cons on both approaches? The data needs to be also mutated quite often, does it alter the verdict?
推荐答案
您可以通过创建一个表示两个模型中的字段的类型来实现此目的.例如:
You can achieve this by creating a type which expresses fields from both models. For example:
import graphene
from graphene_django.types import DjangoObjectType
# hybrid type, expresses some characteristics of Member and Group
class UserGroupType(DjangoObjectType):
class Meta:
model = Membership
# date_joined is automatically derived from the membership
# instance, name and id are declared below.
fields = ('id', 'name', 'date_joined', )
id = graphene.ID()
name = graphene.String()
def resolve_id(value_obj, info):
return value_obj.group.pk
def resolve_name(value_obj, info):
return value_obj.group.name
class PersonType(DjangoObjectType):
class Meta:
model = Person
# id and name are automatically derived from the person
# instance, groups is declared below, overriding the
# normal model relationship.
fields = ('id', 'name', 'groups', )
groups = graphene.List(UserGroupType)
def resolve_groups(value_obj, info):
return value_obj.memberships
对于从Graphene的 ObjectType (其中 DjangoObjectType 来表示输出中的字段需要两件事:
For any type built from Graphene's ObjectType (which DjangoObjectType descends from), to express a field in output you need two things:
- 声明字段的类型
- 一种解析器方法,该方法生成要转换为该类型的结果
DjangoObjectType
评估您提供的模型以自动生成这些模型,并使用
DjangoObjectType
evaluates the model you provide it to generate these automatically and uses the fields attribute to let you customize what properties to reveal.
通过自定义 fields
,然后为要添加的内容添加手动prop/resolver,可以使类型返回您想要的任何内容.
By customizing fields
and then adding manual props/resolvers for what you want to add you can make the type return anything you want.
Note the resolvers don't receive self
as the first argument, but instead get a value object. The value object is the return value of your query resolver and is generally an instance of your model or an array of models that matched a filter, etc.
这篇关于石墨烯-django与ManyToMany&直通表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!