从视图运行自定义admin命令 [英] Run custom admin command from view

查看:102
本文介绍了从视图运行自定义admin命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的管理命令,用于发送报告。它通常从cron工作运行。我想要做的是向我的网络应用程序添加一个按钮,当点击时会导致admin命令在那里运行,而不是等待cron作业调用它。我该如何做?我必须调用命令行,如

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

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.

推荐答案

您可以使用 call_command

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

这篇关于从视图运行自定义admin命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆