在典型的Python类中,我可以有一个实例变量,如下所示:
class MyClass():
def __init__(self):
self.myvar = ""有办法用Django管理命令来做到这一点吗?
class Command(BaseCommand):
def handle(self, *args, **options):
# I want to have a class variable here从下面@shredding的回答中可以看出,您如何正确地覆盖__init__,根据Django文档在这里发现的
class Command(BaseCommand):
def __init__(self, stdout=None, stderr=None, no_color=False):
super(Command, self).__init__(stdout, stderr, no_color)
self.myvar = ""发布于 2016-03-01 14:09:33
当然,只需覆盖init方法:
class YourCommand(BaseCommand)
def __init__(self, stdout=None, stderr=None, no_color=False):
super().__init__(stdout, stderr, no_color)
self.your_var = ''
def handle(self, *args, **kwargs):
... logic goes here ...BaseCommand只是一个简单的老Python对象。
https://stackoverflow.com/questions/35724916
复制相似问题