50

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.

cedric
  • 511
  • 1
  • 4
  • 4
  • 2
    As Daniel says you can use `call_command` or structure your code so that both the management command and the view call some third function that actually does the mail sending. – Davor Lucic Jun 06 '11 at 11:26
  • 1
    possible duplicate of [How can I call a custom Django manage.py command directly from a test driver?](http://stackoverflow.com/questions/907506/how-can-i-call-a-custom-django-manage-py-command-directly-from-a-test-driver) – Chillar Anand Sep 15 '15 at 13:21

1 Answers1

85

You can use call_command:

from django.core.management import call_command
call_command('myadmincmd')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895