我正在尝试使用Wagtail API来提供我的D3插件。我想发送嵌套的ClusterableModels的JSON输出。我在models.py中创建了两组Snippets。一个是包含节点的图形,另一个是嵌套节点的图形:
@register_snippet
class Graph(ClusterableModel):
name = models.CharField(max_length=255)
panels = [
FieldPanel('name'),
InlinePanel('graph_nodes', label='Select Nodes'),
]
def __str__(self):
return '{}'.format(self.name)
class Meta:
verbose_name = 'Graph'
verbose_name_plural = 'Graphs'
@register_snippet
class Node(ClusterableModel, Orderable):
name = models.CharField(max_length=255)
body = models.TextField(blank=True, null=True)
panels = [
FieldPanel('name'),
FieldPanel('body'),
InlinePanel('node_to_node' , label='connect to ...'),
]
def __str__(self):
return '{} ({} links)'.format(self.name, self.node_to_node.count())
class NodeRelations(Orderable, ClusterableModel):
parent = ParentalKey(Node, related_name='node_to_node', null=True, blank=True)
node = models.ForeignKey(Node, related_name="+",
on_delete=models.CASCADE, blank=True, null=True)
panels = [
SnippetChooserPanel('node'),
]
class GraphNodeRelations(Orderable):
parent = ParentalKey(Graph, related_name='graph_nodes', null=True, blank=True)
node = models.ForeignKey(Node, related_name='+',
on_delete=models.SET_NULL, blank=True, null=True)
panels = [
SnippetChooserPanel('node'),
]
问题是Wagtail没有ClusterableModel或Snippets的标准API。但是,它使用“BaseAPIEndpoint”提供自定义endpoints
。我成功地找到了内置的“PagesAPIEndpoint”代码,该代码可以提供有关该过程的洞察力,但我很难想出一个简单地获取所请求的图形(我的图形)及其嵌套子节点(节点)的工作代码