2

I want to run a command which is something like this, but the function handle (self,*args,**options) doesn't seem to execute the nested functions.

How can I include my functions inside handle()?

from django.core.management.base import NoArgsCommand

class Command(NoArgsCommand):
    def handle(self, *args, **options):
        def hello():
            print "hello1"
        def hello1():
            print "hello2"
1
  • 2
    You must 'call' the functions after their definitions in order to execute them: hello(); hello1() Commented Apr 20, 2012 at 8:56

1 Answer 1

3

You can also define functions 'on the fly':

class Command(NoArgsCommand):
    def handle_noargs(self):
        def hello():
            print "hello1"

        def hello1():
            print "hello2"

        hello()
        hello1()

or outside the command (as they where 'service' functions):

def hello():
    print "hello1"

def hello1():
    print "hello2"


class Command(NoArgsCommand):

    def handle_noargs(self):
        hello()
        hello1()
Sign up to request clarification or add additional context in comments.

2 Comments

:hey thanks a lot.. it really helped....one more doubt....if i want to call a function that takes an argument from command line then how i am i supposed to do that ????.Do i have to use class Command or some other class.
Yes, you must extend BaseCommand and use 'handle' method. See here (docs.djangoproject.com/en/dev/howto/custom-management-commands) for two ways to have arguments: "*args" or "option_list"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.