I know this question is old, but today (Django 1.9) the elements of Django history are more reliable than the date of this question. In the current project, I needed to get the last elements of the story and put them in a drop-down list from the navigation panel. Here is how I did it and was very simple:
*views.py* from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION def main(request, template): logs = LogEntry.objects.exclude(change_message="No fields changed.").order_by('-action_time')[:20] logCount = LogEntry.objects.exclude(change_message="No fields changed.").order_by('-action_time')[:20].count() return render(request, template, {"logs":logs, "logCount":logCount})
As you can see from the above code snippet, I am creating a basic set of queries from the LogEntry model (django.contrib.admin.models.py is the place where it is in django 1.9) and excluding elements that have no changes, ordering it in time actions and displaying only the last 20 logs. I also get another counting item. If you look at the LogEntry model, you can see the field names that Django used to discard the pieces of data needed. For my specific case, here is what I used in my template:
Link to the image of the final product
*template.html* <ul class="dropdown-menu"> <li class="external"> <h3><span class="bold">{{ logCount }}</span> Notification(s) </h3> <a href="{% url 'index' %}"> View All </a> </li> {% if logs %} <ul class="dropdown-menu-list scroller actionlist" data-handle-color="#637283" style="height: 250px;"> {% for log in logs %} <li> <a href="javascript:;"> <span class="time">{{ log.action_time|date:"m/d/Y - g:ia" }} </span> <span class="details"> {% if log.action_flag == 1 %} <span class="label label-sm label-icon label-success"> <i class="fa fa-plus"></i> </span> {% elif log.action_flag == 2 %} <span class="label label-sm label-icon label-info"> <i class="fa fa-edit"></i> </span> {% elif log.action_flag == 3 %} <span class="label label-sm label-icon label-danger"> <i class="fa fa-minus"></i> </span> {% endif %} {{ log.content_type|capfirst }}: {{ log }} </span> </a> </li> {% endfor %} </ul> {% else %} <p>{% trans "This object doesn't have a change history. It probably wasn't added via this admin site." %}</p> {% endif %} </li> </ul>
dave4jr Mar 06 '16 at 21:58 2016-03-06 21:58
source share