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第三方库


热门话题
请求响应模式的java JMS   java如何从SeleniumWebDriver中的“Span Type下拉列表”中选择值   java JPA相当于Hibernate的@Generated(GenerationTime.ALWAYS)   java ActiveMQ查询:如何启动   java如何在jsp文件中使用freemarker标记库?   java如何在TabsLayout中用另一个片段替换一个片段的内容?   国际化如何理解Java教程国际化定制资源包加载示例?   XML配置中的java spring log4j无法为日志信息创建文件   oracle11g JDK 6:有没有办法运行一个新的java进程来执行指定类的主方法   java在avro模式中使用“default”   缺陷跟踪我在哪里可以提交关于FOSS Java Sound实施组织的缺陷报告。类路径。冰岛。pulseaudio。Pulseaudiosourcedaline?   编码Java系统。在和系统中。Sun/Oracle和IBM JVM解决方案在读写标准字节数组方面存在差异   java如何将列表项作为查询参数添加到字符串url中?   将自定义库导出到可导入jar文件java