I am deleting objects in different models with DeleteView in Django.
The problem is that I don't want the objects to be completely deleted but rather just hidden. First I thought that it made sense to keep my views as they are but instead overriding the delete method in each model to do as follows
def delete(self, force=False):
if force:
return super(ModelName, self).delete()
else:
self.is_deleted = True
self.save()
but then I noticed that the delete method wont be called in bulk deletion so this method will be too risky.
Can someone recommend a good way to do this? I still want to keep the normal behaviour of DeleteView but it should just 'deactivating' the objects rather than deleting them.
DeleteView is as follows:
def delete(self, request, *args, **kwargs):
"""
Calls the delete() method on the fetched object and then
redirects to the success URL.
"""
self.object = self.get_object()
success_url = self.get_success_url()
self.object.delete()
return HttpResponseRedirect(success_url)
Will it be sufficient if I replace self.object.delete()
with
self.object.is_deleted = True
self.object.save()
When I have marked my objects as deleted, how can I make sure that my querysets wont contain the deleted objects? I could simply replace get_queryset() in my ListView but they should be left out of any queryset on the page so I wonder if I would get better results if I customize the objects manager instead?
I've been looking at django-reversion. Could I simply just delete all objects in the normal manner and then use django-reversion if I want to restore them? Are there any disadvantages of this solution?