Skip to content

Show diff in history_view #1308

Closed
Closed
@davidtoulmin

Description

@davidtoulmin

Problem Statement
Having a list of historical snapshots with no way to compare them in the admin page is difficult to make comparisons, eg. when tracking down when a particular field changed.

Describe the solution you'd like
I would like the ability to see the diff between one historical object and the previous one in the history_view in the admin site. I have the code I've written to do this and a screen shot attached below that demonstrate what I'm after. I'd also be very happy to workshop this into a fully integrated solution and submit a merge request for this feature if people think it's a helpful feature.

Describe alternatives you've considered
I've looked for a way to do this with the codebase, and can't find any, I think this is new functionality. It could be done on its own separate page I could build, but this is what the admin site is for. Users could also just manually go through each change to find the one where the value they're looking for changes, but that's tedious and difficult.

Additional context
Here's the code I've written to achieve this, I've overridden the history_view method, and inserted this code just after the code that sets the history_list_display values (equivalent to line 72 in simple_history/admin.py)

        previous = None
        for list_entry in reversed(action_list):
            if previous:
                diff = list_entry.diff_against(previous).changes
                value = render_to_string("admin/my_app/history_admin_diff.html", {"diff": diff})
            else:
                value = ""
            setattr(list_entry, "diff", value)
            previous = list_entry

        history_list_display = ["diff"] + history_list_display

with the template "admin/my_app/history_admin_diff.html" containing this:

<table>
    <thead><th>Field</th><th>Old Value</th><th>New Value</th></thead>
    <tbody>
    {% for change in diff %}
    <tr>
        <td>{{ change.field }}</td>
        <td>{{ change.old }}</td>
        <td>{{ change.new }}</td>
    </tr>
    {% endfor %}
    </tbody>
</table>

And that creates (for my particular model) a view that looks like this:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions