我有一个使用物化路径来映射文件夹结构的应用程序。现在我想更改它,以便可以在多个位置逻辑链接文件夹(因此一个文件夹可以有多个父级)。这可以用 TypeORM 实现吗?我读了很多文档,但没有找到合适的。
我是Django CMS的新手,我尽力避免询问,但这个让我发疯.我创建了一个带有Topic和Category模型的Wiki应用程序.我将它连接到CMS上的网站并将其添加到我的菜单中.现在,我希望能够在我的菜单上显示所有顶级类别,他们的子类别和主题,以及这些类别的子类别等.
Menu/Navigation should look like this:
Wiki
Category1
Category1.1
Topic
Category1.2
Topic
Category2
Topic
Category3
...
Right now i can only show the Top categories:
Wiki
Category1
Category2
Category3
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个menu.py来获取我的Wiki上的自定义子菜单(您在上面看到的那个):
menu.py
class WikiSubMenu(CMSAttachMenu):
name = _("Wiki Sub-Menu")
def get_nodes(self, request):
nodes = []
categories = Category.objects.filter(parent_id__isnull=True)
for c in categories:
node = NavigationNode(
mark_safe(c.name),
c.get_absolute_url(),
c.id,
)
nodes.append(node)
return nodes
menu_pool.register_menu(WikiSubMenu)
Run Code Online (Sandbox Code Playgroud)
我的分类型号:
class Category(models.Model):
''' Category model. '''
name = models.CharField(max_length=100)
slug = models.SlugField(unique=True)
description = models.TextField(blank=True)
parent = …Run Code Online (Sandbox Code Playgroud) 如https://www.slideshare.net/mongodb/webinar-working-with-graph-data-in-mongodb 中所示,幻灯片 50可以$graphLookup在视图上使用以获得 2 级深度树结构在嵌套格式。
我有一个带有树节点的 MongoDB 集合作为具有以下格式的文档:
{ "_id" : { "$oid" : "5b1a952361c6fa3418a15660" },
"nodeId" : 23978995,
"name" : "settings",
"type" : "Node",
"parentId" : [ 23978893, 23979072, 23979081 ] }
Run Code Online (Sandbox Code Playgroud)
我创建了一个视图,如:
db.createView("treeView", "node", [
{
$graphLookup: {
from: "node",
startWith: "$nodeId",
connectFromField: "nodeId",
connectToField: "parentId",
maxDepth: 0,
as: "children"
}
}
]);
Run Code Online (Sandbox Code Playgroud)
我执行图形查找,如:
db.node.aggregate([
{ $match: {"nodeId": 23978786 } },
{
$graphLookup: {
from: "treeView",
startWith: "$nodeId",
connectFromField: "nodeId", …Run Code Online (Sandbox Code Playgroud) 我想动态构建层次结构,每个节点创建为层次结构中的层/级别,具有自己的节点数组。这应该形成一个树结构。应该有一个根节点,以及未定义数量的节点和级别来构成层次结构的大小。除了根节点之外,不应修复任何内容。我不需要阅读或搜索层次结构,我需要构建它。该数组应以 {"name" : "A", "children" : []} 开头,并且将创建每个新节点作为级别 {"name" : "A", "children" : [HERE-{"name" : “A”,“孩子”:[]}]}。在子数组中,越陷越深。基本上,数组在调用之前应该没有值,除了根节点之外。函数调用之后,数组应包含所需节点的数量,这些节点的数量可能随每次调用而变化,具体取决于数据库查询的结果。每个子数组将包含一个或多个节点值。至少应有 2 个节点级别(包括根)。它最初应该是一个空白画布,没有预定义的数组值。
javascript loops dynamic-data tree-structure javascript-objects
我正在编写一个TreeStructure(TS)类,它允许我创建彼此链接的父对象和子对象。每个 TS 对象都有m_parent属性,它由属性控制parent,并且它们也有children列表,它保存该父对象的所有子对象。每当我将一个孩子添加到其父母的children列表中时,它也会添加到它自己的children列表中吗?这是我所拥有的:
ROOT = "__ROOT__"
class TreeStructure:
def __init__(self, parent=ROOT, children=[]):
self.children = children
self.parent = parent
@property
def parent(self):
'''Returns m_parent'''
if hasattr(self, "m_parent"):
return self.m_parent
else:
return None
@parent.setter
def parent(self, parent=ROOT):
'''Sets m_parent'''
if type(parent) == type(self):
if self.parent:
del self.parent
self.m_parent = parent
self.m_parent.children.append(self)
elif parent == ROOT:
if self.parent:
del self.parent
self.m_parent = ROOT
else:
raise TypeError("Parent's type %s did not match …Run Code Online (Sandbox Code Playgroud) 任何节点都可以包含任意数量的子节点.为了搜索这棵树我写了这样的东西
function Search(key, nodes){
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].key == key) {
return nodes[i];
}
if (nodes[i].hasOwnProperty('children')) {
return this.Search(key, nodes[i].children);
}
}
Run Code Online (Sandbox Code Playgroud)
哪个不太有效...任何输入?
parent-child ×2
django-cms ×1
dynamic-data ×1
javascript ×1
loops ×1
menu ×1
mongodb ×1
parent ×1
python ×1
python-3.x ×1
recursion ×1
tree-search ×1
typeorm ×1