[Django]-Storing user activity in Django


Normally you can use Django Admin Logs for such an activity, if you want.

Normally Django keeps track of admin actions such as creating, updating or deleting existing records. It has the following structure:

from django.contrib.admin.models import LogEntry

                            user_id = ...,
                            content_type_id = ...,
                            object_id = ...,
                            object_repr = ....,
                            change_message = ...,
                            action_flag = ...

I am using that in my system as a logger, and keeping track of every action. Normally, Django logs insert, update or delete operations done over admin forms and I log my hand written view and form actions. Also, you can catch user operations such as login/logout using signals.

I defined new action flags. Django uses 3 flags: 1 for insert, 2 for update and 3 for delete. I expanded that list with my action flags.

The advantage of using this is, as I said, you do not need to handle default Django Admin forms and any action you did using these forms.



You might want to look at django-activity-stream which is an implementation of the activity streams spec. This stores a list of actions in the database and allows the following of users/entities to give something similar to Facebook if this is what you are interested in.

However, as you mention, this can end up with enormous sets of data which might be a bit much for a conventional single database approach. I’m not sure how sites like Twitter deal with it but unless you plan to scale up very quickly the standard database approach would probably last you a while.

Leave a comment