相关疑难解决方法(0)

创建JSON以使用mptt在Python/Django中反映树结构的最快方法

Python(Django)中基于Django查询集创建JSON的最快方法是什么.请注意,在此处提议的模板中解析它不是一个选项.

背景是我创建了一个循环遍历树中所有节点的方法,但在转换大约300个节点时已经非常慢.我想到的第一个(也可能是最糟糕的)想法是以某种方式"手动"创建json.请参阅下面的代码.

#! Solution 1 !!#
def quoteStr(input):
    return "\"" + smart_str(smart_unicode(input)) + "\""

def createJSONTreeDump(user, node, root=False, lastChild=False):
    q = "\""

    #open tag for object
    json = str("\n" + indent + "{" +
                  quoteStr("name") + ": " + quoteStr(node.name) + ",\n" +
                  quoteStr("id") + ": " + quoteStr(node.pk) + ",\n" +
                )

    childrenTag = "children"
    children = node.get_children()
    if children.count() > 0 :
        #create children array opening tag
        json += str(indent + quoteStr(childrenTag) + ": [")
        #for …
Run Code Online (Sandbox Code Playgroud)

python django tree json profiling

18
推荐指数
2
解决办法
1万
查看次数

标签 统计

django ×1

json ×1

profiling ×1

python ×1

tree ×1