创建django管理命令

时间:2012-04-20 08:35:41

标签: django django-admin python-2.7

我想运行类似这样的命令,但函数handle (self,*args,**options)似乎不执行嵌套函数。

如何在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 个答案:

答案 0 :(得分:3)

您还可以“动态”定义功能:

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

        def hello1():
            print "hello2"

        hello()
        hello1()

或在命令之外(因为'service'功能):

def hello():
    print "hello1"

def hello1():
    print "hello2"


class Command(NoArgsCommand):

    def handle_noargs(self):
        hello()
        hello1()