Search code examples
djangodjango-generic-viewsdjango-reversion

How to write a revision for Gerenic DeleteView visible in the admin (Django Reversion)?


I can view all deleted objects when I delete an object in the Django Admin with Reversion installed. But I can't see the deleted object when I delete it in my self-written (gerenic) view. (I'm using the context manager. I can see the changes in the history when I update an object in my generic views.)

This is how I tried to do it (all the classes and methods aren't modified):

class ModelDelete(DeleteView):
    def delete(self, request, *args, **kwargs):
        with transaction.atomic(), reversion.create_revision():
            reversion.set_user(request.user)
            reversion.set_comment('Deleted')
            return super(ModelDelete, self).delete(request, *args, **kwargs))

I'm using Django 1.7.10 and Reversion 1.9.3.

I can't find how to do that in the docs. What do I miss? I think it's quite easy.


Solution

  • Deleting an object does not add it to a revision. Try saving the object before deleting it.

        with transaction.atomic(), reversion.create_revision():
            obj = self.get_object()
            obj.save() # save the object so it is added to the revision.
            reversion.set_user(request.user)
            reversion.set_comment('Deleted')
            return super(ModelDelete, self).delete(request, *args, **kwargs))