3

I am looking for a tool to track user actions like:

  • user logged in
  • user changed password
  • user got bill via email
  • user logged
  • user uploaded image
  • user send message ... which I can include into my Django project. Afterwards I want to build queries and ask the system stuff like:
  • how often did a user a message within a month
  • how often did a user login within a month
  • does the user uploaded any images

and I would like to have some kind of interface. (Like google analytics)

Any idea? I am pretty sure that this is a common task, but I could not find anything like this.

Philipp S.
  • 827
  • 17
  • 41
  • Maybe [this answer](https://stackoverflow.com/questions/987669/tying-in-to-django-admins-model-history/988202#988202) might have more resources to help you – Rafael Santos Sep 24 '22 at 19:52

2 Answers2

1

There are many ways to achieve that. Try reading this link first. Also, you can use LogEntry for tracking the creation, deletion, or changes of the models you have. Also, it shows you the information you need in the admin panel, or also you can use some other third-party packages.
Or you may want to create your own Model to create logs for your application and this link may help you, but do not reinvent the wheel and analyze your situation.

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

LogEntry.objects.log_action(
    user_id=request.user.pk,
    content_type_id=get_content_type_for_model(object).pk,
    object_id=object.pk,
    object_repr=force_text(object),
    action_flag=ADDITION
)
Shayan
  • 238
  • 4
  • 11
  • 1) I don't want to store the user actions the the django logs, because I want to query often information about a users. 2) Where can I see information about a user in the admin panel while using LogEntry? 3) I know I could build somthing on my own, but I think this is a common task and there is something ready to use out there. – Philipp S. Sep 23 '22 at 06:30
1
  1. Create a model to store the user actions. OP will want the Action model to have at least two fields - user (FK to the user model) and action (user action).

    from django.db import models
    
    class Action(models.Model):
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        action = models.CharField(max_length=255)
    
  2. Create a way to store the user actions.

    def store_user_action(user, action):
        action = Action(user=user, action=action)
        action.save()
    
  3. Then if one wants to store when the user changed password, one will go to the view that deals with the change password and call our method store_user_action(request.user, 'changed password') when successful.

  4. To then visualise the user actions, OP can see the records in the Django Admin, create views and templates, ... There are different possibilities.

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83