Search code examples
django-simple-history

Preventing Deletion with django-simple-history


I started using django-simple-history in order to keep the history but when I delete an object (from admin page at least) I notice that it is gone for good.

I suppose I could create tags and "hide" objects instead of deleting in my views but would be nice if there is an easier way with django-simple-history, which would also cover admin operations.


Solution

  • When objects are deleted, that deletion is also recorded in history. The object does not exist anymore, but its history is safe.

    If you browse your database, you should find a table named:

    [app_name]_history[model_name] 
    

    It contains a line with the last state of the object. That line also contains additional columns: history_id, history_change_reason, history_date, history_type. For a deletion, history_type will be set to "-" (minus sign).

    Knowing that, it is possible to revert a deletion programmatically, but not through the Django Admin. Have a look at django-simple-history documentation for details on how to do it programmatically.

    Hope that helps!