Amita Singh
Amita Singh

Reputation: 107

Creating django management commands

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"

Upvotes: 2

Views: 2017

Answers (1)

Don
Don

Reputation: 17606

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()

Upvotes: 3

Related Questions