#display changes in a template view using django-simple-history

1 messages · Page 1 of 1 (latest)

neon locust
#

Hello! Does anyone work a lot with django-simple-history in their projects? I want to have a template view where I can display all the changes that have been made (not using django-admin interface).
I have defined a model and using history = HistoricalRecords() on that model.
Now I would want to display the changes that have been made on that model in my template, but I'm not exactly sure how to set this up. My end goal would be to have a table that loops over all the changes that have been made, so if I have a table of Cars, I would also like to have a "history" column in that table, and if I click on a specific car it redirects me to another view with all the history regarding that car. And let's say the car name has been changes, I only want to display that.
For example - Tezla has been change to Tesla then the table row would display something like this: Tezla ----> Tesla.

Thanks for any help.

manic cove
neon locust
#

Thanks for replying. And sure have, but fairly new to this world. So basically what I'm looking for is a "History Diffing". But can't figure it out...

My views:

def index(request):
    poll = Poll.objects.all()
    return render(request, 'django_history/index.html', {
        'poll':poll,
    })

def history(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)
    p = poll.history.all()

    return render(request, 'django_history/historik.html', {
        'poll': poll,
        'p': p,
    })

The historik.html template:

   <h2>Test history: {{poll.question}}</h2>
 
    {% if history_delta %}
    <h3>Changes made:</h3>
    <table>
        <tr>
            <th>Field</th>
            <th>New</th>
            <th>Old</th>
        </tr>
        
        {% for change in delta.changes %}
            <tr>
                <td>{{change.field}}</td>
                <td>{{change.new}}</td>
                <td>{{change.old}}</td>
            </tr>
        {% endfor %}
    </table>
    {% else %}
    <p>No changes found</p>
    {% endif %}
 

Want to think it shouldn't be hard at all but

manic cove
#

I don't see history_delta being set anywhere in the history view.

#

It would make it easier to help you, if you clarify whether this code was copied over from the docs, or generated, or anywhere else.

neon locust
#

Tried with some help from the AI hence the history_delta....

Here's an updated:

    <h3>Changes made:</h3>
    <table>
        <thead>
            <tr>
                <th>Field</th>
                <th>history id</th>
                <th>Old</th>
            </tr>
        </thead>
        <tbody>
            {% for change in p %}
            <tr>
                <td>{{change.question}}</td>
                <td>{{change.history_id}}</td>
                <td>{{change.old}}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

Now in that table I get 5 rows with all the changes that have been made to the question. But as I stated above, how do I display the changes in one line. And exactly what that have been changed.

Appreciate your help 🙂

manic cove
#

That becomes a display issue.

#

You will need to loop over the history.
And for every record in the history, render something that makes it obvious that all these changes happened in that record.

neon locust
#

Not perfect yet but now I see the changes that have been made on one line:
views:

def history(request, poll_id):
    poll = Poll.objects.get(pk=poll_id)
    p = poll.history.all()
    
    changes = []
    if p is not None and poll_id:
        last = p.first()
        for all_changes in range(p.count()):
            new_record, old_record = last, last.prev_record
            if old_record is not None:
                delta = new_record.diff_against(old_record)
                changes.append(delta)
            last = old_record
    

    return render(request, 'django_history/historik.html', {
        'poll': poll,
        'p': p,
        'changes': changes,
    })

template:

       {% for change in changes %}
                <tr>
                    <td>{{change.new_record.id}}</td>
                    <td>{{change.new_record.history_date}}</td>
                    <td>
                        
                        {% if change.changed_fields|length > 0 %}
                        
                        {% for change_by_id in change.changes  %}
                        <b>{{change_by_id.field|title}}</b> was changed from:
                        
                        {% if change_by_id.old %}
                            <b>{{change_by_id.old}}</b>
                            {% else %}
                            <b>blank field</b>
                            
                        {% endif %}
                            for <b>{{change_by_id.new}}</b>
                            <br>
                        {% endfor %}
                    {% else %}
                    There was no changes in this edition
                        {% endif %}
                    </td>
                    <td>{{change.new_record.history_user}}</td>
                </tr>
            {% endfor %}

Thanks again for some tips 🙂

manic cove
#

I'm glad it's working out! Always remember: Official Docs > AI !