欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django中使用 Closure Table 儲(chǔ)存無限分級(jí)數(shù)據(jù)

 更新時(shí)間:2019年06月06日 14:20:52   作者:棲遲于一丘  
對(duì)于數(shù)據(jù)量大的情況(比如用戶之間有邀請(qǐng)鏈,有點(diǎn)三級(jí)分銷的意思),就要用到 closure table 的結(jié)構(gòu)來進(jìn)行存儲(chǔ)。這篇文章主要介紹了Django中使用 Closure Table 儲(chǔ)存無限分級(jí)數(shù)據(jù),需要的朋友可以參考下

這篇文章給大家介紹Django中使用 Closure Table 儲(chǔ)存無限分級(jí)數(shù)據(jù),具體內(nèi)容如下所述:

起步

對(duì)于數(shù)據(jù)量大的情況(比如用戶之間有邀請(qǐng)鏈,有點(diǎn)三級(jí)分銷的意思),就要用到 closure table 的結(jié)構(gòu)來進(jìn)行存儲(chǔ)。那么在 Django 中如何處理這個(gè)結(jié)構(gòu)的模型呢?

定義模型

至少是要兩個(gè)模型的,一個(gè)是存儲(chǔ)分類,一個(gè)儲(chǔ)存分類之間的關(guān)系:

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")

數(shù)據(jù)操作

獲得所有后代節(jié)點(diǎn)

class Category(models.Model):
 ...
 def get_descendants(self, include_self=False):
 """獲得所有后代節(jié)點(diǎn)"""
 kw = {
 'descendants__ancestor' : self
 }
 if not include_self:
 kw['descendants__distance__gt'] = 0
 qs = Category.objects.filter(**kw).order_by('descendants__distance')
 return qs獲得直屬下級(jí)
class Category(models.Model):
 ...
 def get_children(self):
 """獲得直屬下級(jí)"""
 qs = Category.objects.filter(descendants__ancestor=self, descendants__distance=1)
 return qs

節(jié)點(diǎn)的移動(dòng)

節(jié)點(diǎn)的移動(dòng)是比較難的,在 [ https://www.percona.com/blog/2011/02/14/moving-subtrees-in-closure-table/][1 ] 中講述了,利用django能夠執(zhí)行原生的sql語句進(jìn)行:

def add_child(self, child):
 """將某個(gè)分類加入本分類,"""
 if CategoryRelation.objects.filter(ancestor=child, descendant=self).exists() \
 or CategoryRelation.objects.filter(ancestor=self, descendant=child, distance=1).exists():
 """child不能是self的祖先節(jié)點(diǎn) or 它們已經(jīng)是父子節(jié)點(diǎn)"""
 return
 # 如果表中不存在節(jié)點(diǎn)自身數(shù)據(jù)
 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};
 """)

 節(jié)點(diǎn)刪除

節(jié)點(diǎn)刪除有兩種操作,一個(gè)是將所有子節(jié)點(diǎn)也刪除,另一個(gè)是將自己點(diǎn)移到上級(jí)節(jié)點(diǎn)中。

擴(kuò)展閱讀

[ 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):
 """獲得所有后代節(jié)點(diǎn)"""
 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):
 """獲得直屬下級(jí)"""
 qs = Category.objects.filter(descendants__ancestor=self, descendants__distance=1)
 return qs
def get_ancestors(self, include_self=False):
 """獲得所有祖先節(jié)點(diǎn)"""
 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):
 """分類僅有一個(gè)父節(jié)點(diǎn)"""
 parent = Category.objects.get(ancestors__descendant=self, ancestors__distance=1)
 return parent
def get_parents(self):
 """分類僅有一個(gè)父節(jié)點(diǎn)"""
 qs = Category.objects.filter(ancestors__descendant=self, ancestors__distance=1)
 return qs
def remove(self, delete_subtree=False):
 """刪除節(jié)點(diǎn)"""
 if delete_subtree:
 # 刪除所有子節(jié)點(diǎn)
 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:
 # 所有子節(jié)點(diǎn)移到上級(jí)
 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):
 """將某個(gè)分類加入本分類,"""
 if CategoryRelation.objects.filter(ancestor=child, descendant=self).exists() \
 or CategoryRelation.objects.filter(ancestor=self, descendant=child, distance=1).exists():
 """child不能是self的祖先節(jié)點(diǎn) or 它們已經(jīng)是父子節(jié)點(diǎn)"""
 return
 # 如果表中不存在節(jié)點(diǎn)自身數(shù)據(jù)
 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

總結(jié)

以上所述是小編給大家介紹的Django中使用 Closure Table 儲(chǔ)存無限分級(jí)數(shù)據(jù),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Tensorflow中的placeholder和feed_dict的使用

    Tensorflow中的placeholder和feed_dict的使用

    這篇文章主要介紹了Tensorflow中的placeholder和feed_dict的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Python數(shù)據(jù)分析之?Matplotlib?散點(diǎn)圖繪制

    Python數(shù)據(jù)分析之?Matplotlib?散點(diǎn)圖繪制

    這篇文章主要介紹了Python數(shù)據(jù)分析之?Matplotlib?散點(diǎn)圖繪制,散點(diǎn)圖又稱散點(diǎn)圖,是使用多個(gè)坐標(biāo)點(diǎn)的分布反映數(shù)據(jù)點(diǎn)分布規(guī)律、數(shù)據(jù)關(guān)聯(lián)關(guān)系的圖表,下文對(duì)散點(diǎn)圖的詳細(xì)介紹及繪制,需要的小伙伴可以參考以一下
    2022-05-05
  • vscode使用chatGPT 的方法

    vscode使用chatGPT 的方法

    ChatGPT是基于GPT-3模型的對(duì)話生成模型,可以根據(jù)上下文生成自然語言文本來模擬人類對(duì)話,這篇文章主要介紹了vscode使用chatGPT ,需要的朋友可以參考下
    2022-12-12
  • python獲取一組漢字拼音首字母的方法

    python獲取一組漢字拼音首字母的方法

    這篇文章主要介紹了python獲取一組漢字拼音首字母的方法,涉及Python針對(duì)漢字操作的相關(guān)技巧,需要的朋友可以參考下
    2015-07-07
  • Python中else的三種使用場(chǎng)景

    Python中else的三種使用場(chǎng)景

    在Python中else最常見的用法就是用在判斷語句中,其實(shí)還可以用在循環(huán)語句和異常處理中。 下面來總結(jié)一下else的用法:
    2021-06-06
  • python中時(shí)間模塊的基本使用教程

    python中時(shí)間模塊的基本使用教程

    這篇文章主要給大家介紹了關(guān)于python中時(shí)間模塊的基本使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • python 引用傳遞和值傳遞詳解(實(shí)參,形參)

    python 引用傳遞和值傳遞詳解(實(shí)參,形參)

    這篇文章主要介紹了python 引用傳遞和值傳遞詳解(實(shí)參,形參)。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python中的類型提示(Type Hints)總結(jié)

    Python中的類型提示(Type Hints)總結(jié)

    Python3.5 版本引入了類型提示(Type Hints),它允許開發(fā)者在代碼中顯式地聲明變量、函數(shù)、方法等的類型信息,下面小編就來帶大家一起看看Python類型提示的初步使用吧
    2023-05-05
  • Python 下載Bing壁紙的示例

    Python 下載Bing壁紙的示例

    這篇文章主要介紹了Python 下載Bing壁紙的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • Python函數(shù)式編程

    Python函數(shù)式編程

    函數(shù)式編程Functional Programming,雖然也可以歸結(jié)到面向過程的程序設(shè)計(jì),但其思想更接近數(shù)學(xué)計(jì)算。函數(shù)式編程就是一種抽象程度很高的編程范式,純粹的函數(shù)式編程語言編寫的函數(shù)沒有變量。
    2017-07-07

最新評(píng)論