软删除介绍
简单的说,就是当执行删除操作的时候,不真正执行删除操作,而是在逻辑上删除一条记录。这样做的好处是可以统计数据,可以进行恢复操作等等。
Managers 是django models 提供的一个用于提供数据库查询操作的接口,对于Django应用程序中的每个model都会至少存在一个Manager
详细:https://docs.djangoproject.com/zh-hans/3.2/topics/db/managers/
django实现软删除model
from django.db import models
from django.db.models.query import QuerySet
# 自定义软删除查询基类
class SoftDeletableQuerySetMixin(object):
"""
QuerySet for SoftDeletableModel. Instead of removing instance sets
its ``is_deleted`` field to True.
"""
def delete(self):
"""
Soft delete objects from queryset (set their ``is_deleted``
field to True)
"""
self.update(is_deleted=True)
class SoftDeletableQuerySet(SoftDeletableQuerySetMixin, QuerySet):
pass
class SoftDeletableManagerMixin(object):
"""
Manager that limits the queryset by default to show only not deleted
instances of model.
"""
_queryset_class = SoftDeletableQuerySet
def get_queryset(self):
"""
Return queryset limited to not deleted entries.
"""
kwargs = {'model': self.model, 'using': self._db}
if hasattr(self, '_hints'):
kwargs['hints'] = self._hints
return self._queryset_class(**kwargs).filter(is_deleted=False)
class SoftDeletableManager(SoftDeletableManagerMixin, models.Manager):
pass
# 自定义软删除抽象基类
class SoftDeletableModel(models.Model):
"""
An abstract base class model with a ``is_deleted`` field that
marks entries that are not going to be used anymore, but are
kept in db for any reason.
Default manager returns only not-deleted entries.
"""
is_deleted = models.BooleanField(default=False)
class Meta:
abstract = True # 成功发送Signal信号需要将abstract设置为true
auto_created = False
objects = SoftDeletableManager()
def delete(self, using=None, soft=True, *args, **kwargs):
"""
Soft delete object (set its ``is_deleted`` field to True).
Actually delete object if setting ``soft`` to False.
"""
if soft:
self.is_deleted = True
self.save(using=using)
else:
return super(SoftDeletableModel, self).delete(using=using, *args, **kwargs)
model demo代码开发
class Server(SoftDeletableModel):
id = models.AutoField(primary_key=True)
local_ip = models.CharField('内网ip', max_length=30, blank=True, null=True)
class Meta:
db_table = 'dbmanage_server'
class ServerBase(models.Model):
id = models.AutoField(primary_key=True)
local_ip = models.CharField('内网ip', max_length=30, blank=True, null=True)
is_deleted = models.BooleanField('是否删除', default=False)
class Meta:
managed = False # 在migrate时不会创建该表
db_table = 'dbmanage_server'
继承了软删除模型类SoftDeletableModel的模型server,在进行migrate建表操作时,自动会给Server表添加上一个is_deleted字段。后续的数据删除操作中只需要执行delete(),即可将数据的is_deleted设置为True。查询操作时,不需要加上条件限制,自动会返回is_deleted=False的数据。
Tips
1:Server模型无法发送信号
增加代码abstract = True(具体细节不太了解,看源码里要求的)
2:需要查询已经删除掉的数据
有时候想要展示已经删除的数据,但是Server模型里是自动过滤掉删除数据的。
增加一个ServerBase模型类,在进行查询时使用该类,记得增加managed=Fasle,这样在migrate时不会django不会去创建该表。
网友评论