我想让 Django 的管理命令在每个 Python 表达式执行之前和之后shell
运行一个脚本。例如:
(想想我正在使用命令python3 manage.py shell --ipython
进入 shell。)
In [1]: from api.models import Student
In [2]: new_student = Student.objects.create(name="John Doe", number=123)
In [3]: new_student.number
Out[3]: 123
In [4]: new_student.name
Out[4]: "John Doe"
我想将它们的表达式和输出发送到Slack 频道。为此,我需要在每个表达式之前和之后运行 Python 脚本,该脚本将表达式或输出记录到我的 Slack 频道。
对于上面的示例,脚本会将表达式(例如"new_student.number"
)及其输出(如果存在,当然) (例如"123"
,例如 )记录到 Slack 通道。
我尝试使用django-extensions模块,将其shell_plus
命令与pre 和 post 信号一起使用。但它只是在运行 plus_shell 之前和之后调用信号。因此,我想做的是在每个 command/expression 之前和之后运行这些信号处理程序。
有没有办法使用配置、模块甚至编写自定义管理命令来实现这一点?