Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Django manage.py custom commands return a value? How, or why not?

Tags:

Following the documentation: https://docs.djangoproject.com/en/dev/howto/custom-management-commands/

I created my own custom command (called something else but example shown below):

from django.core.management.base import BaseCommand, CommandError from polls.models import Poll  class Command(BaseCommand):     args = '<poll_id poll_id ...>'     help = 'Closes the specified poll for voting'      def handle(self, *args, **options):         for poll_id in args:             try:                 poll = Poll.objects.get(pk=int(poll_id))             except Poll.DoesNotExist:                 raise CommandError('Poll "%s" does not exist' % poll_id)              poll.opened = False             poll.save()              self.stdout.write('Successfully closed poll "%s"' % poll_id)          return "Yay" 

The question is how come returning a string like "Yay" does not work? Am I doing it wrong or is it not possible?

When I call the custom command from my view, I do something like:

     value = call_command('call_custom_command', parameter)      print value 

but the value is shown to be None.

like image 687
Alf Avatar asked Mar 21 '14 23:03

Alf


People also ask

What does the Django command manage py?

Starts a command line in whatever $SHELL your environment uses. Starts a Django command prompt with your Python environment pre-loaded. Loads a Python command prompt you can use to sync your database schema remotely.

How do you pass arguments in Django management command?

The parameter parser is an instance of argparse. ArgumentParser (see the docs). Now you can add as many arguments as you want by calling parser 's add_argument method. In the code above, you are expecting a parameter n of type int which is gotten in the handle method from options .

What is BaseCommand Django?

BaseCommand is a Django object for creating new Django admin commands that can be invoked with the manage.py script. The Django project team as usual provides fantastic documentation for creating your own commands.


1 Answers

If you want to get the output of call_command(), you need to capture stdout. Here's how you can do it:

out = StringIO() call_command('call_custom_command', stdout=out)  value = out.getvalue() print value 

This technique is actually used in django tests for testing management commands.

Demo:

>>> from django.core.management import call_command >>> from StringIO import StringIO >>> out = StringIO() >>> call_command('validate', stdout=out) >>> out.getvalue() '0 errors found\n' 

Hope that helps.

like image 180
alecxe Avatar answered Sep 18 '22 15:09

alecxe