简体   繁体   中英

custom django-admin commands - method not found

While running a django-admin command - it can't find a local method.

update.py

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

    def doThis():
        test2 = 'hello'

running the command python3 manage.py update produces the error:

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

I can't quite see a reason for this in the documentation https://docs.djangoproject.com/en/1.7/howto/custom-management-commands/#methods

Two things you're missing about how Python handles classes and objects.

Command.handle is calling a method, doThis , from the same class so you should use self :

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

And then you should change the signature of doThis so Python can use it as a method of the class Command :

def doThis(self):
    test2 = 'hello'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM