Django中物化路径树结构的简单轻量级实现。

django-materialized-paths的Python项目详细描述


django物化路径

Django中物化路径树结构的简单轻量级实现。

安装

要安装,请运行:

pip install django-materialized-paths

配置

将以下内容添加到项目的已安装应用程序中:

INSTALLED_APPS=(...'django-materialized-paths',)

用法

要使用,请将BaseNode扩展到模型中:

fromdjango-materialized-pathsimportBaseNodeclassFolderExample(BaseNode):"""My folder class"""name=models.CharField(max_length=48)

该模型重写django的django.db.models.Modelsave()方法,以自动管理对继承的更改:

from.modelsimportFolderExample# concrete class from aboveroot=FolderExample.objects.create(name="root")child=FolderExample.objects.create(name="child",parent=root)grandchild=FolderExample.objects.create(name="child",parent=child)

例如,要将grandchild转换为根:

grandchild.parent=Nonegrandchild.save()

使用-属性和继承

BaseNode.depth# Root is 0BaseNode.parentBaseNode.children# Only returns direct descendantsBaseNode.save()# Set the parent field to automatically manage hierarchyBaseNode.delete()# NOTE: The BaseNode.parent field is set to cascade, so deleting a parent will delete all children 

用法-方法

注意,这些方法通常返回queryset

# AccessorsBaseNode.get_root()BaseNode.get_ancestor(depth=int)BaseNode.get_ancestors()BaseNode.get_descendants()BaseNode.get_siblings()# ConvenienceBaseNode.has_children()BaseNode.is_child_of(parent_id=int)# ID-based accessors - computed directly from path without accessing dbBaseNode.get_ancestors_ids()BaseNode.get_descendants_ids()

作者

许可证

这个项目是在麻省理工学院的许可下授权的-详细信息请参见LICENSE.md文件

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
javajaxb在嵌套节点中生成xmlns:xsi声明   java重定向到youtube以上传jsp上可用的视频   java提高了JVM和ANT的性能   java如何将返回的JSON数据放在HTML表单中   java如何发送XWWWFORMURLENCODE在重新授权   java为什么jvm XX:+EliminateAllocations失败   堆移除和重建方法?(爪哇)   Stanford NPL in Spark(Scala)应用程序运行到Java堆空间(Java.lang.OutOfMemoryError)   调试java中打开/关闭println的任何方式(详细模式)   java IntelliJ在鼠标悬停时显示JavaDocs工具提示   Olingo Odata Java中的自动扩展选项   json将postgres文本arry转换为java列表?   oop需要帮助推断Java代码   复选框的java添加操作侦听器AbstractTableModel   java如何从公共静态void main(String[]args)传递值   java从数据库中获取大值   java Processing 2.0电影从jar运行时无法打开电影文件   java如何在不使用应用程序的情况下刷新活动?   尝试将JSON解析为ListView时的Android:JSON parse:null对象引用