简介:
无限极分类是一种比较常见的数据格式,生成组织结构,生成商品分类信息,权限管理当中的细节权限设置,都离不开无限极分类的管理。
常见的有链表式,即有一个Pid指向上级的ID,以此来设置结构。写的时候简单,用的时候效果一班,比如说,同一级没有办法手动重新排序,查询所有子孙的时候不方便。
所以有了预排序树,即左右值树形管理。
优点还是挺多的。
可以快速确定关系,最短路径,同级排序,查找所有子孙(最好的地方)
一:主要包
sqlalchemy_mptt
https://github.com/uralbash/sqlalchemy_mptt
pip install sqlalchemy_mptt
sqlalchemy-orm-tree
https://github.com/monetizeio/sqlalchemy-orm-tree/
pip install sqlalchemy-orm-tree
最后更新2015年8月27日,还没有完整的示例代码,仅有的几行,还………………
放弃
ztree
http://www.treejs.cn/
这个是前台JS显示用的,希望我能整合到flask-admin中去。
二:sqlalchemy_mptt
1.快速指南
源码:
#!/usr/bin/python3#-*- coding: utf-8 -*-#@Time : 2018-09-12 16:32#@Author : Jackadam#@Email :jackadam@sina.com#@File : mptt.py#@Software: PyCharm
from sqlalchemy importcreate_enginefrom sqlalchemy.orm importscoped_session, sessionmakerfrom sqlalchemy_mptt importmptt_sessionmakerfrom sqlalchemy importColumn, Integer, Stringfrom sqlalchemy.ext.declarative importdeclarative_basefrom sqlalchemy_mptt.mixins importBaseNestedSets
Base=declarative_base()classTree(Base, BaseNestedSets):__tablename__ = "tree"id= Column(Integer, primary_key=True)
name= Column(String(8))def __repr__(self):return "" %self.id
engine= create_engine('sqlite:///mptt.db', echo=False)
mptt_ession= mptt_sessionmaker(sessionmaker(bind=engine))
db_session= scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))def print_tree(group_name, tab=1):""":param str group_name:要查找的树的根的名称
:param int tab: 格式化用的-数量"""group= db_session.query(Tree).filter_by(name=group_name).one_or_none() #type: TreeGroup
if notgroup:return
#group found - print name and find children
print('-' * tab +group.name)for child_group in group.children: #type: TreeGroup
#new tabulation value for child record
print_tree(child_group.name, tab * 2)if __name__ == '__main__':#Base.metad