2016-08-31 47 views
1

運行django-admin命令時 - 找不到本地方法。自定義django-admin命令 - 未找到方法

update.py

class Command(NoArgsCommand): 
    help = 'Help Test' 
    def handle(self, **options): 
     test1 = 'hello' 
     doThis() 

    def doThis(): 
     test2 = 'hello' 

運行命令python3 manage.py update產生錯誤:

File "/opt/dir/app/management/commands/updatefm.py", line 25, in handle 
     doThis() 
    NameError: name 'doThis' is not defined 

我不能完全明白了一個道理這個文檔中https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/#methods

回答

0

兩個你錯過了關於Python如何處理類和對象的東西。

Command.handle被調用的方法,doThis,來自同一類,所以你應該使用self

def handle(self, **options): 
    test1 = 'hello' 
    self.doThis() 

然後你應該改變的doThis的簽名,以便Python可以使用它作爲類Command的方法:

def doThis(self): 
    test2 = 'hello'