Jamgreen
Jamgreen

Reputation: 11039

Mark as deleted instead of actually deleting in DeleteView

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?

Upvotes: 2

Views: 1401

Answers (2)

GwynBleidD
GwynBleidD

Reputation: 20539

but then I noticed that the delete method wont be called in bulk deletion so this method will be too risky.

you can write your own QuerySet for that and use it as_manager. Same QuerySet can take care of hiding your deleted fields from displaying. Remember to leave some way for retrieving all of your deleted fields.

Upvotes: 1

user764357
user764357

Reputation:

When I have marked my objects as deleted, how can I make sure that my querysets wont contain the deleted objects?

As the comment states, the Django-only solution is writing a customer Manager that understands your is_deleted field.

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?

Yes, as long as your wrap your deletions in reversions. This can be as simple as using the reversion middleware to wrap all deletes and saves:

MIDDLEWARE_CLASSES = (
    'reversion.middleware.RevisionMiddleware',
    # Other middleware goes here...
)

Are there any disadvantages of this solution?

None that I've found, its well supported and apart from just deletion support it also has version tracking.

Upvotes: 1

Related Questions