Django中使用 Closure Table 储存无限分级数据
作者:栖迟于一丘 发布时间:2021-05-25 03:05:07
标签:django,Closure,Table,分级数据
这篇文章给大家介绍Django中使用 Closure Table 储存无限分级数据,具体内容如下所述:
起步
对于数据量大的情况(比如用户之间有邀请链,有点 * 分销的意思),就要用到 closure table 的结构来进行存储。那么在 Django 中如何处理这个结构的模型呢?
定义模型
至少是要两个模型的,一个是存储分类,一个储存分类之间的关系:
class Category(models.Model):
name = models.CharField(max_length=31)
def __str__(self):
return self.name
class CategoryRelation(models.Model):
ancestor = models.ForeignKey(Category, null=True, related_name='ancestors', on_delete=models.SET_NULL, db_constraint=False, verbose_name='祖先')
descendant = models.ForeignKey(Category,null=True, related_name='descendants', on_delete=models.SET_NULL,
db_constraint=False, verbose_name='子孙')
distance = models.IntegerField()
class Meta:
unique_together = ("ancestor", "descendant")
数据操作
获得所有后代节点
class Category(models.Model):
...
def get_descendants(self, include_self=False):
"""获得所有后代节点"""
kw = {
'descendants__ancestor' : self
}
if not include_self:
kw['descendants__distance__gt'] = 0
qs = Category.objects.filter(**kw).order_by('descendants__distance')
return qs获得直属下级
class Category(models.Model):
...
def get_children(self):
"""获得直属下级"""
qs = Category.objects.filter(descendants__ancestor=self, descendants__distance=1)
return qs
节点的移动
节点的移动是比较难的,在 [ https://www.percona.com/blog/2011/02/14/moving-subtrees-in-closure-table/][1 ] 中讲述了,利用django能够执行原生的sql语句进行:
def add_child(self, child):
"""将某个分类加入本分类,"""
if CategoryRelation.objects.filter(ancestor=child, descendant=self).exists() \
or CategoryRelation.objects.filter(ancestor=self, descendant=child, distance=1).exists():
"""child不能是self的祖先节点 or 它们已经是父子节点"""
return
# 如果表中不存在节点自身数据
if not CategoryRelation.objects.filter(ancestor=child, descendant=child).exists():
CategoryRelation.objects.create(ancestor=child, descendant=child, distance=0)
table_name = CategoryRelation._meta.db_table
cursor = connection.cursor()
cursor.execute(f"""
DELETE a
FROM
{table_name} AS a
JOIN {table_name} AS d ON a.descendant_id = d.descendant_id
LEFT JOIN {table_name} AS x ON x.ancestor_id = d.ancestor_id
AND x.descendant_id = a.ancestor_id
WHERE
d.ancestor_id = {child.id}
AND x.ancestor_id IS NULL;
""")
cursor.execute(f"""
INSERT INTO {table_name} (ancestor_id, descendant_id, distance)
SELECT supertree.ancestor_id, subtree.descendant_id,
supertree.distance+subtree.distance+1
FROM {table_name} AS supertree JOIN {table_name} AS subtree
WHERE subtree.ancestor_id = {child.id}
AND supertree.descendant_id = {self.id};
""")
节点删除
节点删除有两种操作,一个是将所有子节点也删除,另一个是将自己点移到上级节点中。
扩展阅读
[ https://www.percona.com/blog/2011/02/14/moving-subtrees-in-closure-table/][2 ]
[ http://technobytz.com/closure_table_store_hierarchical_data.html][3 ]
完整代码
class Category(models.Model):
name = models.CharField(max_length=31)
def __str__(self):
return self.name
def get_descendants(self, include_self=False):
"""获得所有后代节点"""
kw = {
'descendants__ancestor' : self
}
if not include_self:
kw['descendants__distance__gt'] = 0
qs = Category.objects.filter(**kw).order_by('descendants__distance')
return qs
def get_children(self):
"""获得直属下级"""
qs = Category.objects.filter(descendants__ancestor=self, descendants__distance=1)
return qs
def get_ancestors(self, include_self=False):
"""获得所有祖先节点"""
kw = {
'ancestors__descendant': self
}
if not include_self:
kw['ancestors__distance__gt'] = 0
qs = Category.objects.filter(**kw).order_by('ancestors__distance')
return qs
def get_parent(self):
"""分类仅有一个父节点"""
parent = Category.objects.get(ancestors__descendant=self, ancestors__distance=1)
return parent
def get_parents(self):
"""分类仅有一个父节点"""
qs = Category.objects.filter(ancestors__descendant=self, ancestors__distance=1)
return qs
def remove(self, delete_subtree=False):
"""删除节点"""
if delete_subtree:
# 删除所有子节点
children_queryset = self.get_descendants(include_self=True)
for child in children_queryset:
CategoryRelation.objects.filter(Q(ancestor=child) | Q(descendant=child)).delete()
child.delete()
else:
# 所有子节点移到上级
parent = self.get_parent()
children = self.get_children()
for child in children:
parent.add_chile(child)
# CategoryRelation.objects.filter(descendant=self, distance=0).delete()
CategoryRelation.objects.filter(Q(ancestor=self) | Q(descendant=self)).delete()
self.delete()
def add_child(self, child):
"""将某个分类加入本分类,"""
if CategoryRelation.objects.filter(ancestor=child, descendant=self).exists() \
or CategoryRelation.objects.filter(ancestor=self, descendant=child, distance=1).exists():
"""child不能是self的祖先节点 or 它们已经是父子节点"""
return
# 如果表中不存在节点自身数据
if not CategoryRelation.objects.filter(ancestor=child, descendant=child).exists():
CategoryRelation.objects.create(ancestor=child, descendant=child, distance=0)
table_name = CategoryRelation._meta.db_table
cursor = connection.cursor()
cursor.execute(f"""
DELETE a
FROM
{table_name} AS a
JOIN {table_name} AS d ON a.descendant_id = d.descendant_id
LEFT JOIN {table_name} AS x ON x.ancestor_id = d.ancestor_id
AND x.descendant_id = a.ancestor_id
WHERE
d.ancestor_id = {child.id}
AND x.ancestor_id IS NULL;
""")
cursor.execute(f"""
INSERT INTO {table_name} (ancestor_id, descendant_id, distance)
SELECT supertree.ancestor_id, subtree.descendant_id,
supertree.distance+subtree.distance+1
FROM {table_name} AS supertree JOIN {table_name} AS subtree
WHERE subtree.ancestor_id = {child.id}
AND supertree.descendant_id = {self.id};
""")class CategoryRelation(models.Model): ancestor = models.ForeignKey(Category, null=True, related_name='ancestors', on_delete=models.SET_NULL, db_constraint=False, verbose_name='祖先') descendant = models.ForeignKey(Category,null=True, related_name='descendants', on_delete=models.SET_NULL, db_constraint=False, verbose_name='子孙') distance = models.IntegerField()
class Meta:
unique_together = ("ancestor", "descendant")[1]: https://www.percona.com/blog/2011/02/14/moving-subtrees-in-closure-table/
[2]: https://www.percona.com/blog/2011/02/14/moving-subtrees-in-closure-table/
[3]: http://technobytz.com/closure_table_store_hierarchical_data.html
总结
以上所述是小编给大家介绍的Django中使用 Closure Table 储存无限分级数据网站的支持!
来源:https://www.hongweipeng.com/index.php/archives/1802/


猜你喜欢
- python 3.x版本print输出不换行的格式如下:print(x, end="")其中,end=&quo
- 测试字符串:<style>v\:* { BEHAVIOR: url(#default#VML) } o\:* { BEHAVIO
- 前言SQLSERVER 2005中不知因何去掉了很重要的DEBUGGER功能,要调试,必须要安装VS2005专业版或者更高版本。非常不方便。
- 1.计算长度value = "wangdianchao"# 计算字符个数(长度)number = len(value)p
- 本文主要用python实现了对网站的模拟登录。通过自己构造post数据来用Python实现登录过程。当你要模拟登录一个网站时,首先要搞清楚网
- 一、数学相关1、绝对值:abs(-1)2、最大最小值:max([1,2,3])、min([1,2,3])3、序列长度:len('ab
- 先看下面例子的效果:<INPUT TYPE="text" NAME=""&
- 1.实现效果2.实现原理echarts官网:series-lines注意:流动特效只支持非平滑曲线(smooth:false)series-
- Pattern.split方法详解/** * 测试Pattern.split方法 */ @Test public void testPatt
- 直接pip install impala 是不行滴,按照以下步骤安装就会成功!一路安装就可以1、pip install six2、pip i
- 前言本文总结了mysql中DCL,常用的一些权限控制,后续使用到其他会继续补充。一、用户控制管理创建用户create user '用
- 加了三个验证漏洞以及四个getshell方法# /usr/bin/env python3# -*- coding: utf-8 -*-# @
- SQL中的declare用法平时写SQL查询、存储过程都是凭着感觉来,没有探究过SQL的具体语法,一直都是按c#那一套往SQL上模仿,前几天
- MySQL 创建数据库和创建数据表MySQL 是最常用的数据库,在数据库操作中,基本都是增删改查操作,简称CRUD。在这之前,需要先安装好
- 题目:转换RBG颜色值我们知道在网页中的颜色值设置都是用16进制的RGB来表示的,比如#FFFFFF,表示R:255,G:255,B:255
- 分页显示是页面常用技术,可用下列代码来实现:<%page=Request.QueryString("page&q
- 如下所示:def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=
- 前言本文提供将图片色彩转为黑白或者褐色风格。比较类似于我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。环
- 英文文档:callable(object)Return True if the object argument appears callab
- 第一种:ROW_NUMBER() OVER()方式select * from ( select *, ROW_NUMBER() OVER(O