我想知道是否有更好的方法来添加新节点到树中?现在看起来是这样的:
...
# Add new data to DB
for new_record in new_records:
# Find appropriate parent using data in 'new_record'
parent = get_parent(new_record)
# Create object which should be added using data in 'new_record'
new_node = MyMPTTModel(...)
new_node.insert_at(parent, save = True)
# Similar to:
# new_node.insert_at(parent, save = False)
# new_node.save()
但它的工作速度非常慢。我认为它是这样工作的,因为在每次调用
insert_at(..., save = True)
方法后,django-mptt
都应该将新节点写入数据库,并修改已经在数据库中的记录的left
和right
键。是否有办法使django-mptt
每次调用insert_at
时修改查询,然后在调用save
时一起应用所有更改?或者你知道其他减少执行时间的方法吗?提前致谢。