如何将 Neomodel 关系数据转换为 JSON 以将其传递给 Cytoscape.js

问题描述 投票:0回答:0

我正在尝试在我的 Django 模板中显示 Neo4J 图形数据。对于查询,我使用 neomodel 和 Cytoscape.js 进行图形可视化。我能够以 cytoscape.js 需要的 JSON 形式转换节点数据,但对于边缘,我不知道该怎么做。

这是我的模特

class Player(StructuredNode):
    name = StringProperty(required=True)
    age = IntegerProperty(required=True)
    height = IntegerProperty(required=True)
    number = IntegerProperty(required=True)
    weight = IntegerProperty(required=True)
    team = RelationshipTo('Team', 'PLAYS_FOR', model=PlayerTeamRel)

    @property
    def serialize(self):
        return {
            'data': {
                'id': self.id,
                'name': self.name,
                'age': self.age,
                'height': self.height,
                'number': self.number,
                'weight': self.weight,
            }
        }

class Team(StructuredNode):
    name = StringProperty(required=True)
    players = RelationshipFrom('Player', 'PLAYS_FOR')
    coach = RelationshipFrom('Coach', 'COACHES')

    @property
    def serialize(self):
        return {
            'data': {
                'id': self.id,
                'name': self.name,
                'players': self.players,
                'coach': self.coach
            }
        }

class Coach(StructuredNode):
    name = StringProperty(required=True)
    coach = RelationshipTo('Team', 'COACHES')

    @property
    def serialize(self):
        return {
            'data': {
                'id': self.id,
                'name': self.name,
                'coach': self.coach
            }
        }

serialize() 方法用于获取 JSON 格式。

当我尝试在控制台中打印时,该关系属性(团队)的类型为 ZeroToOne。我想要以下格式的 JSON,特别是关系(边缘)数据,因为我能够序列化和显示节点数据。

{ data: { id: 'a' } },
  { data: { id: 'b' } },
  { data: { id: 'c' } },
  { data: { id: 'd' } },
  { data: { id: 'e' } },
  { data: { id: 'f' } },
  // edges
  {
    data: {
      id: 'ab',
      source: 'a',
      target: 'b'
    }
  },
  {
    data: {
      id: 'cd',
      source: 'c',
      target: 'd'
    }
  },
  {
    data: {
      id: 'ef',
      source: 'e',
      target: 'f'
    }
  },
  {
    data: {
      id: 'ac',
      source: 'a',
      target: 'c'
    }
  },
  {
    data: {
      id: 'be',
      source: 'b',
      target: 'e'
    }
  }

我没有发现 cytoscape.js 和 neomodel 数据之间有任何适当的集成,也找不到检索关系数据的方法。谁能建议我这样做的方法。

python django neo4j cytoscape.js neomodel
© www.soinside.com 2019 - 2024. All rights reserved.