Is it possible to use the built-in LogEntry to track actions of every users not only in admin page?

Question:

I just recently knew about the built in LogEntry model in django, and I’ve tried to registered it in my admin page. What I want to happen is, for example, the admin added new User not using the django admin page but through the add page I made using some model forms in my site, how can i add data to LogEntry regarding that add action on my site? Thanks for your help!

Asked By: newbie programmer

||

Answers:

You can easily do that in outside the admin by creating a new LogEntry object each time a change is made

from django.contrib.admin.models import LogEntry, ADDITION, CHANGE # these are action flags from the docs
LogEntry.objects.log_action(
        user_id=request.user.id,
        content_type_id=ContentType.objects.get_for_model(model_object).pk,
        object_repr=unicode(obj.title), #or any field you wish to represent here
        object_id=obj.id,
        message=message, # a new user has been added
        action_flag=ADDITION) # assuming it's a new object

You can read more LogEntry module in the docs

There is much more information on this here

Easy Way!

from django.contrib.admin.models import LogEntry
from datetime import datetime, timedelta


et = datetime.now()
st = et + timedelta(days=-1)

LogEntry.objects.log_action(action_time__range=[st, et])
Answered By: Deepanshu Mehta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.