0

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.

yofee
  • 1,287
  • 12
  • 25

1 Answers1

0

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))
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Will that result in displaying the objects in the list when I click on "Recover deleted MyModels" in the admin (for the "history" is gone in the admin when I delete an object)? – yofee Sep 06 '15 at 15:05
  • 1
    The object should appear in "Recover deleted MyModels" whether or not you create a revision when deleting. By creating a revision when it is deleted, you log who deleted the object. – Alasdair Sep 06 '15 at 15:51
  • Thanks! Now I also saw that the deleted objects are visible in that list, at the end. By the way, `self.object` isn't availabe in `def delete`. – yofee Sep 06 '15 at 17:10
  • Looking at [the code](https://github.com/django/django/blob/1.8.4/django/views/generic/edit.py#L289), the `delete()` method fetches the object with `self.object = self.get_object()`, so you'll have to do the same in your overridden method. I've updated the code above. – Alasdair Sep 06 '15 at 17:36