Loading [MathJax]/jax/output/CommonHTML/config.js
社区首页 >问答首页 >Django MPTT - objects.rebuild有多贵?

Django MPTT - objects.rebuild有多贵?
EN

Stack Overflow用户
提问于 2013-05-13 06:29:35
回答 1查看 997关注 0票数 1

在接下来的几天里,我将推出一个使用Django MPTT管理分层数据的应用程序。MPTT提供了一个名为rebuild的函数,它可以重新构建给定模型可用的所有树,并被称为TreeNodes.objects.rebuild()。如您所见,该命令是在模型上调用的,而不是在模型的实例上调用的。此命令必须在将节点插入到树中后调用。

对于Django MPTT 0.6 (还没有正式的released),实现了一个partial_rebuild命令,它只会重新构建给定的树。

虽然在本地测试多达10棵树完全没有性能问题,但我担心当我的数据库中有100棵树,并且我调用rebuild命令(它将重建所有100棵树)时,这可能是一个严重的性能问题。

有谁有使用rebuild命令的经验吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-15 14:56:55

以备将来参考..

只有在某些特殊情况下才需要使用objects.rebuild()。通常,mptt只需设置节点的父id,即可正确设置节点的左值和右值。docs中也提到了这一点

我遇到了一个没有正确设置左、右值的问题,因为我保存了新节点,!之前!我重置了另一个已经存在的同级的位置值。在为设置了元属性order_insertion_by的树插入新节点时,必须首先重置所有同级的order_insertion_by值,然后保存新节点。这样,mptt就能够正确地重新计算左侧和右侧的值。

参见下面我的(简化)示例:

models.py

代码语言:javascript
代码运行次数:0
复制
class Node(MPTTModel):
    """
    Representation of a single node
    """
    name = models.CharField(max_length=200)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='%(app_label)s_%(class)s_children')
    position = models.PositiveIntegerField(max_length=10) #for nodes on the same hierarchy level we have to define the position in which they are displayed 

    class MPTTMeta:
        order_insertion_by = ['position']

views.py

代码语言:javascript
代码运行次数:0
复制
new_node = Node(name="new node", parent=parent_node, position=1)
update_node_positions(new_node, 1) #routine to update the node positions
new_node.save()

update_node_positions

代码语言:javascript
代码运行次数:0
复制
def update_node_positions(node, mode):
    """
    Procedure to update the node positions
    Three different modes available:
        1 = insert node
        2 = update position, parent stays the same
        3 = update position and change parent
        4 = trashed
    """

    if mode == 1 or mode==3:
        #if node has been inserted at the beginning
        if node.position == 1:
            node.get_siblings().update(position=F('position') + 1)
        #if node has been inserted not at beginning and not at the last position
        elif node.position != node.get_siblings().count() + 1:
            #update positions of siblings right of node by one
            node.get_siblings().filter(position__gte=node.position).update(position=F('position') + 1)
        if mode == 3:
            #since we removed the node from a parent, we have to decrement the positions of the former siblings right of the node by one
            if node._original_parent is not None:
                #do updates only for nodes which had a parent before. will not be executed for root nodes
                node._original_parent.get_children().filter(position__gt=node._original_position).update(position=F('position') - 1)
    if mode == 2:
        #if old position is left of new position -> decrement position by 1 for nodes which have position <= node.position AND > node.original_position
        if node.position > node._original_position:
            node.get_siblings().filter(Q(position__lte=node.position) & Q(position__gt=node._original_position)).update(position=F('position') - 1)
        #if old position is right of new position -> increment position by 1 for nodes which have position >= node.position AND < node.original_position 
        if node.position < node._original_position:
            node.get_siblings().filter(Q(position__gte=node.position) & Q(position__lt=node._original_position)).update(position=F('position') + 1)
    if mode == 4:
        #decrement position by 1 for nodes which have position > node.position
        node.get_siblings().filter(Q(position__gt=node.position)).update(position=F('position') - 1)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16516032

复制
相关文章
Vue 中,如何将函数作为 props 传递给组件
作者:Michael Thiessen 译者:前端小智 来源:medium 点赞再看,养成习惯本文 GitHub https://github.com/qq44924588... 上已经收录,更多往
前端小智@大迁世界
2020/05/18
8.2K0
React技巧之将函数作为props传递
原文链接:https://bobbyhadz.com/blog/react-typescript-pass-function-as-prop[1]
chuckQu
2022/08/19
9620
React技巧之将函数作为props传递
HTML中传递和引用JavaScript变量
http://ivantian2008.blog.51cto.com/622133/1127456
明哥的运维笔记
2019/01/30
5.6K0
Python 函数作为参数传递
#map()的功能是将函数对象依次作用于表的每一个元素,每次作用的结果储存于返回的表re中。 #map通过读入的函数(这里是lambda函数)来操作数据 def test_func_map():     re = map((lambda x: x+3), [1, 2, 3, 4])     print re def testA(a, b, **kargs):     print a+b     print "testA: %s" % kargs #函数作为参数传递 def test_func(func, a, b, **kargs):     func(a, b)     print "test_func: %s" % kargs #函数作为参数传递 def test_func_lambda(func, **kargs):     func()     print "test_func_lambda: %s" % kargs def test_func_getattr():     func = getattr(obj, "testA")     func(1, 2) class TestGetattr():     aa = "2a"     def get_attr(self):         print "test getattr()"     def print_text(self):         print "print text"     def print_string(self):         print "print string" #getattr(obj, "a")的作用和obj.a是一致的,但该方法还有其他的用处,最方便的就是用来实现工厂方法 #根据传入参数不同,调用不同的函数实现几种格式的输出 def output(print_type="text"):     tg = TestGetattr()     output_func = getattr(tg, "print_%s" % print_type)     output_func() if __name__ == "__main__":     #test_func(testA, 1, 2, aa="aa")     #test_func_lambda((lambda: testA(1, 2, bb="bb")), cc="cc")     #test_func_map()     #test_func_getattr()     #getattr方法,传入参数是对象和该对象的函数或者属性的名字,返回对象的函数或者属性实例     obj = TestGetattr()     func = getattr(obj, "get_attr") #getattr()获得对象的属性和方法     func()     print getattr(obj, "aa") #完成对象的反射     print obj.aa     #callable方法,如果传入的参数是可以调用的函数,则返回true,否则返回false。     print callable(getattr(obj, "aa"))     output("string")
py3study
2020/01/09
3K0
【Python】函数进阶 ③ ( 函数作为参数传递 )
之前介绍的函数 , 都是 接收具体的 变量 或 字面量 数据 作为参数 , 如 : 数字 / 布尔值 / 字典 / 列表 / 元组 等 ;
韩曙亮
2023/10/11
4550
【Python】函数进阶 ③ ( 函数作为参数传递 )
Go-函数作为参数传递
编码过程中业务需要将一个函数,作为参数传递到函数内部。Go 语言的匿名函数是一个闭包(Closure)
王小明_HIT
2023/03/01
1.7K0
Go-函数作为参数传递
React技巧之将CSS作为props传递
原文链接:https://bobbyhadz.com/blog/react-pass-style-as-props-typescript[1]
chuckQu
2022/08/19
2.5K0
React技巧之将CSS作为props传递
thymeleaf 传递数据到js变量
thymeleaf 传递数据到js变量 如何把控制器传来的model中的值传递给js变量呢? 需要以下两个: <script th:inline="javascript"> var message =
Dream城堡
2018/09/10
5.1K0
python中函数嵌套、函数作为变量以及闭包的原理
python允许创建嵌套函数。也就是说我们可以在函数里面定义函数,而且现有的作用域和变量生存周期依旧不变。
狼啸风云
2021/03/04
5.3K0
python中函数嵌套、函数作为变量以及闭包的原理
JS处理函数将对象作为参数传递
做项目的时候遇到一个不是很常见的问题,就是js函数传递参数的时候,我们一般是传递一个数字或者是一个字符串,但是当你的需求满足不了的时候,就需要将对象或者数组作为一个参数传递过去,这个时候怎么做呢,今天简单的说有一下:
何处锦绣不灰堆
2020/05/29
7.1K0
C++返回vector/将vector作为参数传递
在C++里很多时候我们会遇到函数想返回两个以上结果的情况,这时候可以用数组(vector)、类来作为容器返回,也可以声明一个全局变量的数组,将数值存放在数组里解决。
vincentbbli
2021/08/18
5.5K0
详解JavaScript中的变量提升/函数提升
先有鸡还是先有蛋:直觉上会认为 JavaScript 代码在执行时是由上到下一行一行执行的。但实际上这并不完全正确,有一种特殊情况会导致这个假设是错误的。
用户10106350
2022/10/28
1.5K0
答网友问:golang中的slice作为函数参数时是值传递还是引用传递?
今天有网友问通道和切片在赋值给另一个变量或作为函数参数传递的时候是不是引用传递?因为老师在讲解的时候说是指针传递?
Go学堂
2023/08/29
7160
答网友问:golang中的slice作为函数参数时是值传递还是引用传递?
C语言指针变量作为函数参数
自学气象人
2023/06/20
1160
C语言指针变量作为函数参数
WordPress 技巧:千万不要在全局中把 $blog_id 作为变量名
所以在 WordPress MU 环境中,在进行一些操作的时候,比如需要切换博客进行设置数据,千万不要把 $blog_id 作为变量名。
Denis
2023/04/15
2540
javascript——函数、变量和方法
当代码出现有规律的重复之后,可以利用函数,定义变量,调用方法,不用去重复的改动代码,只需要进行函数的修改。基本上所有的高级语言都支持函数,javascript也不例外,它可以像变量一样被使用,方便且强大,因此本文对js函数进行系统的学习,并在学习过程中做了详细的笔记以及样例。
子舒
2022/06/09
1.2K0
javascript——函数、变量和方法
Swift 5.2 将实例作为函数调用
Swift 5.2中的一个新功能是可以将类型实例作为函数调用(callAsFunction)。或者,如Swift Evolution 提案所述,“用户定义的标称类型的可调用值”。此函数的简短描述是,它允许您调用实现了callAsFunction方法的任何类型的实例,就好像它是一个函数一样。
韦弦zhy
2020/03/19
2.4K0
golang-101-hacks(12)——切片作为函数参数传递
注:本文是对golang-101-hacks中文翻译。 在Go语言中,函数参数是值传递。使用slice作为函数参数时,函数获取到的是slice的副本:一个指针,指向底层数组的起始地址,同时带有slice的长度和容量。既然各位熟知数据存储的内存的地址,现在可以对切片数据进行修改。让我们看看下面的例子: In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now. Let's see the following example:
羊羽shine
2019/06/05
1.2K0
python中的id( )函数
>>> a=2.0 >>> b=2.0 >>> id(a) 524440880 >>> id(b) 524440904 >>> a=2 >>> b=2 >>> id(a) 524425104 >>> id(b)524425104为什么上面输出的值有些一样,有些不一样呢,求大神详细解释下。
用户7886150
2021/01/23
1K0
下篇1:将 ConfigMap 中的键值对作为容器的环境变量
继续接上篇,《一文了解K8S的ConfigMap》。上篇聊过,官方文档中提到的可以使用下面4种方式来使用 ConfigMap 配置 Pod 中的容器:
不背锅运维
2023/05/26
2.2K0
下篇1:将 ConfigMap 中的键值对作为容器的环境变量

相似问题

JQuery\Javascript -将函数作为变量传递

90

将jQuery ID作为参数传递给JavaScript函数

47

jquery noUiSlider将外部javascript变量传递到函数中。

12

将变量作为对javascript/jquery函数的引用传递

20

将字段id作为javascript函数的变量传递时出错

39
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档