为什么我有“TypeError:'Command'对象不能被解释为整数”?

时间:2017-08-24 20:07:49

标签: python django

我正在编写一个管理命令来在django应用程序中安装一些测试数据。我的一个功能是给我一个错误:

  

TypeError:'Command'对象不能解释为整数

以下是导致它的代码:

import string
from my_app.models import Customer
from random import choice

class Command(BaseCommand):

    def random_string(size=4, chars=string.ascii_lowercase + string.digits + "    "):
            """ Creates a random string of letters and digits """
            return ''.join(choice(chars) for _ in range(0, size))

    def handle(self, *args, **options):
        c = Customer(firstname=self.random_string(), foo=bar)

如果我在调用pdb.set_trace()的位置self.random_string(),我会注意到我无法拨打self.random_string()但是''.join(choice(string.ascii_lowercase) for _ in range(0, 4))可以正常工作。

那里出了什么问题?

完整追踪:

  File "/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/application/utils/management/commands/generate_test_data.py", line 263, in handle
    firstname=self.random_string(),
  File "/application/utils/management/commands/generate_test_data.py", line 64, in random_string
    return ''.join(choice(chars) for _ in range(0, size))

0 个答案:

没有答案