Python Graphene 处理多对多关系
Posted
技术标签:
【中文标题】Python Graphene 处理多对多关系【英文标题】:Python Graphene working with many to many relations 【发布时间】:2020-03-02 06:18:05 【问题描述】:如果这在其他地方得到了回答,那么我很抱歉,但下班后 2 天仍然没有雪茄......
我有一个播放器模型:
class Player(models.Model):
name = models.CharField(max_length=60)
discord_id = models.CharField(max_length=60, null=True)
known_npcs = models.ManyToManyField(NPC)
玩家可以认识很多NPC,任何NPC都可以被很多玩家认识。
NPC没什么特别的:
class NPC(models.Model):
image = models.ImageField()
name = models.CharField(max_length=50)
description = models.TextField()
谜题的最后一部分是事实 事实是附加到 NPC 的一些信息,但是一个人可以知道 NPC,但不一定所有与 NPC 相关的事实都被玩家知道,因此事实看起来像这样:
class Fact(models.Model):
fact = models.TextField()
known_by = models.ManyToManyField(Player)
npc = models.ForeignKey(NPC, on_delete=models.DO_NOTHING, null=True)
现在在石墨烯中,我想创建一个 Player 和 allPlayers 查询,它会给我这个:
allPlayers
name
knownNPCs
image
name
description
factsKnown
fact
factsKnown 仅是基于 Fact 对象的 ManyToMany 关系的事实。
到目前为止,我创建的内容会返回数据,但不会根据玩家父级过滤 Facts 仅显示与 npc 相关的所有事实 :(
事实架构
class FactType(DjangoObjectType):
class Meta:
model = Fact
filter_fields = ["id"]
class Query(object):
fact = Node.Field(FactType)
all_Facts = graphene.List(FactType)
def resolve_all_Facts(self, info, **kwargs):
return Fact.objects.all()
NPCSchema
class NPCType(DjangoObjectType):
class Meta:
model = NPCS
class Query(object):
all_NPCs = graphene.Field(NPCType)
facts = graphene.List(FactType)
def resolve_all_NPCs(self, info, **kwargs):
return NPCS.objects.all()
PlayerSchema:
class PlayerType(DjangoObjectType):
class Meta:
model = Player
interfaces = (Node,)
filter_fields = ["id"]
class Query(object):
player = Node.Field(PlayerType)
all_players = graphene.List(PlayerType)
def resolve_all_players(self, info, **kwargs):
return Player.objects.all()
def resolve_player(self, info, **kwargs):
player = Player.objects.filter(id=info.id)
【问题讨论】:
如果你执行 allPlayers name knownNPCs image name description factSet fact
会发生什么?
@Roel 仍然返回未在 known_by 对象中分配的事实:/ 为了让它更有趣,它知道连接因为返回 ``` "allPlayers": [ "id": "1", “knownNpcs”:[“name”:“XXXXX”,“factSet”:[“fact”:“YYYYY”,“playerSet”:[“id”:“2”]],```
【参考方案1】:
所以对于仍在阅读的任何人,我设法通过创建一个“filteredFacts”字段来解决这个问题:
class PlayerType(DjangoObjectType):
class Meta:
model = Player
filter_fields = ["id"]
filtered_facts = graphene.List(FactGroup)
def resolve_filtered_facts(self, info, **kwargs):
groups = defaultdict(list)
facts = self.known_facts.all()
for fact in facts:
groups[fact.npc].append(fact.fact)
grouped_facts = []
for key, value in groups.items():
grouped_facts.append(FactGroup(npc=key, facts=value))
return grouped_facts
这需要玩家知道的所有事实并按 NPC 分组,从另一边得到相同的结果...
【讨论】:
以上是关于Python Graphene 处理多对多关系的主要内容,如果未能解决你的问题,请参考以下文章
如何避免在 SQLAlchemy - python 的多对多关系表中添加重复项?