I am trying to add logging to my Django app using EventLog. I followed an example online but not sure how to pass in the user that makes the changes. The example shows it as user=self.user. Obviously this wouldn't work in my case as it doesn't refer to anything in my model

我正在尝试使用EventLog将日志记录添加到我的Django应用程序中。我在线跟踪了一个示例,但不确定如何传入进行更改的用户。该示例将其显示为user = self.user。显然这在我的情况下不起作用,因为它没有引用我的模型中的任何东西

models.py

class Client(models.Model):
    name = models.CharField(max_length=50)
    ....

    def save(self, *args, **kwargs):
            # Initial Save
            if not self.pk:
                log(user=self.user, action='ADD_CLIENT',
                    extra={'id': self.id})
            else:
                log(user=self.user, action='UPDATED_CLIENT',
                    extra={'id': self.id})

           super(Client, self).save(*args, **kwargs)

1 个解决方案

#1


1

The save method will only know what has been passed into it, this will normally not include the request which is where you would get the current user (request.user).

save方法只会知道传递给它的内容,这通常不会包含您获取当前用户的请求(request.user)。

You should instead add logging in the view which is calling the save method.

您应该在调用save方法的视图中添加日志记录。

user = request.user

更多相关文章

  1. Python文件遍历的三种方法
  2. python的接口实现zope.interface示例
  3. python--继承--方法的重写---和父类的扩展
  4. Python学习/复习神器-->各种方法/技巧在哪用和典型例子(一)
  5. Python测试函数和类 笨方法学习Python
  6. Python列表以及列表的处理方法
  7. 在python中复制命令的正确方法[复制]
  8. 转python爬虫:BeautifulSoup 使用select方法详解
  9. Python------类的结构细分,异常处理,方法,属性

随机推荐

  1. Android五大布局
  2. Android(安卓)proguard混淆编译的问题
  3. Android入门篇一:Android Activity生命周
  4. SDK中模拟器创建命令行!!!
  5. Android自定义dialog以及如何去除黑色背
  6. Android反编译工具介绍
  7. 设置listview的背景颜色
  8. Android——SeekBar(拖动条)相关知识总结贴
  9. android 如何让 EditText 默认不获取焦点
  10. android语音识别之科大讯飞语音API的使用