Adding a Log entry for an action by a user in a Django App

后端 未结 1 1919
说谎
说谎 2021-02-01 17:58

I need to create a log entry for changes made by a user to the database via the views in my django application.

I have enabled the django-admin module and I can retriev

1条回答
  •  走了就别回头了
    2021-02-01 18:56

    You're very close. You just need to create new LogEntry objects and save them. LogEntry has a shortcut function on objects to do this.

    from django.contrib.admin.models import LogEntry, ADDITION, CHANGE
    
    LogEntry.objects.log_action(
                user_id=request.user.id,
                content_type_id=ContentType.objects.get_for_model(model_object).pk,
                object_id=object.id,
                object_repr=unicode(object.title),
                action_flag=ADDITION if create else CHANGE)
    

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