从视图运行自定义管理命令。

时间:2021-01-23 23:17:17

I have a custom admin command that emails out reports. It normally runs from a cron job. What I would like to do is add a button to my web app that when clicked will cause the the admin command to run there and then rather than wait for the cron job to call it. How do I do this? Do I have to call out to a command line such as

我有一个自定义的管理命令来发送报告。它通常由cron工作运行。我想做的是在我的web应用程序中添加一个按钮,当单击时,它将导致管理命令在那里运行,而不是等待cron作业调用它。我该怎么做呢?我是否需要调用命令行,例如

python manage.py myadmincmd 

or can I invoke the code from within a view? It seems it would be cleaner if I could do this from within a view without needing to break out to the command line.

或者我可以从视图中调用代码吗?如果我可以在视图中这样做,而不需要跳转到命令行,看起来会更简洁。

1 个解决方案

#1


45  

You can use call_command:

您可以使用call_command:

from django.core.management import call_command
call_command('myadmincmd')

#1


45  

You can use call_command:

您可以使用call_command:

from django.core.management import call_command
call_command('myadmincmd')