我们正在使用 django-simple-history 来跟踪模型中的更改。所有模型都有一个 history = HistoricalRecords()
字段。从 python shell 更改模型时,系统会跟踪更改,但 changed_by
字段保存为 None
。当在 admin 中进行更改时,simple_history 中间件会从登录者那里获取 User 实例。显然,在 shell 中我们没有这个。有没有办法根据现有的 Account 对象手动注入(inject) User 实例?
不幸的是,我无法更改任何这些模型,因此我无法将任何历史用户 getter 和 setter 添加到我们的模型中(项目经理对重构非常严格,我们也有很多模型)
最佳答案
如docs所示,对于一个名为 ObjectWithHistory
的具有历史记录的特定对象,您可以在保存之前在该对象上设置历史用户,如下所示:
o = ObjectWithHistory(*kwargs)
o._history_user = this_user
o.save()
关于python - django-simple-history 从 python shell 跟踪用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58272381/