【发布时间】:2025-11-22 20:10:02
【问题描述】:
我使用 Django 和石墨烯来构建 API,但我想在一个查询中组合两个模型,模型的所有字段都是相同的。
示例 架构.py
import graphene
from graphene_django import DjangoObjectType
from .models import Post, Post2
class PostType(DjangoObjectType):
class Meta:
model = Post
class Post2Type(DjangoObjectType):
class Meta:
model = Post2
class Query(graphene.ObjectType):
post = graphene.List(PostType)
post2 = graphene.List(Post2Type)
def resolve_post(self, info):
return Post.objects.all()
def resolve_post2(self, info):
return Post2.objects.all()
我收到此回复:
{
"data": {
"post": [
{
"title": "post 1"
}
],
"post2": [
{
"title": "post test"
}
]
}
}
我想得到的是:
{
"data": {
"allPost": [
{
"title": "post 1"
},
{
"title": "post test"
}
}
}
【问题讨论】:
标签: python django graphql graphene-django