|
| 1 | +Admin Integration |
| 2 | +----------------- |
| 3 | + |
| 4 | +To allow viewing previous model versions on the Django admin site, inherit from |
| 5 | +the ``simple_history.admin.SimpleHistoryAdmin`` class when registering your |
| 6 | +model with the admin site. |
| 7 | + |
| 8 | +This will replace the history object page on the admin site and allow viewing |
| 9 | +and reverting to previous model versions. Changes made in admin change forms |
| 10 | +will also accurately note the user who made the change. |
| 11 | + |
| 12 | +.. image:: screens/1_poll_history.png |
| 13 | + |
| 14 | +Clicking on an object presents the option to revert to that version of the object. |
| 15 | + |
| 16 | +.. image:: screens/2_revert.png |
| 17 | + |
| 18 | +(The object is reverted to the selected state) |
| 19 | + |
| 20 | +.. image:: screens/3_poll_reverted.png |
| 21 | + |
| 22 | +Reversions like this are added to the history. |
| 23 | + |
| 24 | +.. image:: screens/4_history_after_poll_reverted.png |
| 25 | + |
| 26 | +An example of admin integration for the ``Poll`` and ``Choice`` models: |
| 27 | + |
| 28 | +.. code-block:: python |
| 29 | +
|
| 30 | + from django.contrib import admin |
| 31 | + from simple_history.admin import SimpleHistoryAdmin |
| 32 | + from .models import Poll, Choice |
| 33 | +
|
| 34 | + admin.site.register(Poll, SimpleHistoryAdmin) |
| 35 | + admin.site.register(Choice, SimpleHistoryAdmin) |
| 36 | +
|
| 37 | +Changing a history-tracked model from the admin interface will automatically record the user who made the change (see :doc:`/user_tracking`). |
| 38 | + |
| 39 | + |
| 40 | +Displaying custom columns in the admin history list view |
| 41 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 42 | + |
| 43 | +By default, the history log displays one line per change containing |
| 44 | + |
| 45 | +* a link to the detail of the object at that point in time |
| 46 | +* the date and time the object was changed |
| 47 | +* a comment corresponding to the change |
| 48 | +* the author of the change |
| 49 | + |
| 50 | +You can add other columns (for example the object's status to see |
| 51 | +how it evolved) by adding a ``history_list_display`` array of fields to the |
| 52 | +admin class |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + from django.contrib import admin |
| 57 | + from simple_history.admin import SimpleHistoryAdmin |
| 58 | + from .models import Poll, Choice |
| 59 | +
|
| 60 | +
|
| 61 | + class PollHistoryAdmin(SimpleHistoryAdmin): |
| 62 | + list_display = ["id", "name", "status"] |
| 63 | + history_list_display = ["status"] |
| 64 | + search_fields = ['name', 'user__username'] |
| 65 | +
|
| 66 | + admin.site.register(Poll, PollHistoryAdmin) |
| 67 | + admin.site.register(Choice, SimpleHistoryAdmin) |
| 68 | +
|
| 69 | +
|
| 70 | +.. image:: screens/5_history_list_display.png |
0 commit comments