TOP

django ORM定义实现链表结构

需求场景

各种链表使用场景, 如单串, 双端链表等

需求描述

实现阶段间串联的可前进后退的关系模型

逻辑分析

节点间串联. 主要需要控制的是前节点和后节点的顺序关系

以及插入或删除节点时的一些操作

代码实现

"""
Stage
"""

from django.db import models


class Stage(models.Model):
    """
    Stage Model
    """

    name = models.CharField("阶段名称", help_text="阶段名称 string", max_length=64)
    remark = models.CharField(
        "备注", help_text="备注", max_length=255, null=True, blank=True
    )
    pre_stage = models.ForeignKey(
        "self",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        help_text="前一阶段",
        verbose_name="前一阶段",
        db_constraint=False,
        related_name="stage_stage_pre_stage",
    )
    next_stage = models.ForeignKey(
        "self",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        help_text="后一阶段",
        verbose_name="后一阶段",
        db_constraint=False,
        related_name="stage_stage_next_stage",
    )

    def __str__(self):
        return self.name

    def save(self,
             force_insert=False,
             force_update=False,
             using=None,
             update_fields=None,
             just_save=False):
        if just_save:
            return super().save()
        """
        1  =====> 1 -> 2      (2 在 1 后面追加)
        3  =====> 2 -> 3      (2 在 3 前面追加)
        1 -> 3  =====> 1 -> 2 -> 3    (2 在 1 和 3 中间追加)
        
        当前节点添加 
            存在前节点, 更新前节点的后节点为当前节点
            存在后节点, 更新后节点的前节点为当前节点
        """
        if self.pre_stage or self.next_stage:
            super().save()
            if self.pre_stage:
                self.pre_stage.next_stage = self
                self.pre_stage.save(just_save=True)
            if self.next_stage:
                self.next_stage.pre_stage = self
                self.next_stage.save(just_save=True)
        else:
            super().save()

    def delete(self, using=None, keep_parents=False, just_delete=False):
        if just_delete:
            return super().delete()
        """
        1 -> 2 -> 3  ====> 1 -> 3
        1 -> 2 -> None  ====> 1 -> None
        None -> 2 -> 
        3  ====> None -> 3
        None -> 2 -> None ====> None
        
        当前节点删除
            存在前节点: 更新前节点的后节点为 None
            存在后节点: 更新后节点的前节点为 None
            前后都存在节点: 前节点和后节点相连(前节点的后节点为后节点, 后节点的前节点为前节点) 
        """
        super().delete()
        # 1 -> 2 -> 3  ====> 1 -> 3
        if self.pre_stage and self.next_stage:
            self.pre_stage.next_stage = self.next_stage
            self.pre_stage.save(just_save=True)
            self.next_stage.pre_stage = self.pre_stage
            self.next_stage.save(just_save=True)
        # 1 -> 2 -> None  ====> 1 -> None
        elif self.pre_stage and not self.next_stage:
            self.pre_stage.next_stage = None
            self.pre_stage.save(just_save=True)
        # None -> 2 -> 3  ====> None -> 3
        elif not self.pre_stage and self.next_stage:
            self.next_stage.pre_stage = None
            self.next_stage.save(just_save=True)
        # None -> 2 -> None ====> None
        else:
            pass

    class Meta:
        verbose_name_plural = "阶段"

 

 

 

posted @ 2022-08-15 19:17  羊驼之歌  阅读(80)  评论(0编辑  收藏  举报