views:

99

answers:

2

Hello I use django admin for my users to add their Model objects, as you know django is keeping the track of user actions such as the user who added an item. For an object, At a custom view outside the admin panel, I need to display the user name of the adder.

How can I fetch/retrieve this data ?

Cheers

+1  A: 

This is just a rough sketch but should be pretty close to what you need for your view:

from django.contrib.admin.models import LogEntry, ADDITION

def your_view(request):
    ...
    # This is your object that was modified
    my_obj = ...
    log_entry = LogEntry.objects.filter(
        object_id=my_obj.id,
        action_flag=ADDITION,
        content_type__id__exact=ContentType.objects.get_for_model(MyModel).id)
    ...

Then in your template:

{{ log_entry.user.username }}
Van Gale
this looks promising, as my item can have several actions, how can I retrieve the "add" action, I have been checking the database structure of "django_admin_log", seems like the "action_flag" keeps the action.
Hellnar
from django.contrib.admin.models import ADDITIONfilter(..., action_flag=ADDITION)
Van Gale
Edited answer to include ADDITION filter
Van Gale
A: 

If you're looking to add additional functionality beyond what was mentioned above, I've made use of an app called django-reversion which in addition to making the log accessible would also provide a copy of what was changed (that incidentally can also be used to generate a DIFF with or without HTML showing the changes).

T. Stone