我使用的是来自django-model-utils的SoftDeletableModel模型,它是一个抽象的基类模型,带有一个is_removed字段,用于标记不再使用但出于任何原因被保存在db中的条目。
它的结构如下:
class SoftDeletableModel(models.Model):
is_removed = models.BooleanField(default=False)
class Meta:
abstract = True
objects = SoftDeletableManager()
all_objects = models.Manager()
def delete(self, using=None, soft=True, *args, **kwargs):
"""
Soft delete object (set its ``is_removed`` field to True).
Actually delete object if setting ``soft`` to False.
"""
if soft:
self.is_removed = True
self.save(using=using)
else:
return super().delete(using=using, *args, **kwargs)如果我真的想删除一个对象,而不是设置is_removed=True,我需要在删除过程中添加soft=False。
我该如何在视图/模板中执行此操作?
发布于 2020-01-06 07:06:32
将soft=False作为关键字参数传递给实例的delete方法:
your_object_instance.delete(soft=False)
这可以在视图函数中完成,也可以在项目中能够访问此对象实例的任何其他python文件中完成。
https://stackoverflow.com/questions/59603842
复制相似问题