Hi I am new to Django and I have just completed CRUD using django and postgresql.
now my aim is to do SOftDelete but I am unable to do it
below is my code
def Delemp(request,id):
delemployee = EmpModel.objects.get(id=id)
delemployee.delete()
showdata=EmpModel.objects.all()
return render(request,"Index.html",{"data":showdata})
I am unable to convert the function in such a way that It would perform softdelete instead of hard delete,please help
CodePudding user response:
Deletion in Django orm; By default, it like to hard delete. For soft delete you have to use a package or write your own manager class.
https://django-safedelete.readthedocs.io/en/latest/ It is easy to implement in a Django project. I recommend. It provides hard-soft delete control with policies.
CodePudding user response:
In your case the soft delete doesn't work, since you don't use the object anymore. If it's still referenced, then it should work. Please look example from the links for details:
# Example of use
>>> article1 = Article(name='article1')
>>> article1.save()
>>> article2 = Article(name='article2')
>>> article2.save()
>>> order = Order(name='order')
>>> order.save()
>>> order.articles.add(article1)
# This **article will be masked**, but not deleted from the database as it is still referenced in an order.
>>> article1.delete()
# This **article will be deleted** from the database.
>>> article2.delete()