Storing user activity in Django

前端 未结 2 1641
暖寄归人
暖寄归人 2021-02-18 22:51

I am looking to store the activity of an user, but I am not sure where to store it. I dont think database is a option as it will be very big than. I am looking to know as to how

相关标签:
2条回答
  • 2021-02-18 23:23

    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.

    0 讨论(0)
  • 2021-02-18 23:26

    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
    
    LogEntry.objects.log_action(
                                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.

    0 讨论(0)
提交回复
热议问题