我正在尝试制作一个自定义管理命令,如下面的文档所示:https ://docs.djangoproject.com/en/dev/howto/custom-management-commands/
当我尝试从我的项目目录运行命令时,我遇到以下错误:
AttributeError: 'module' object has no attribute 'Command'
这是文件:
#event_expiration.py
from django.core.management.base import BaseCommand, CommandError
from app.models import Event
import datetime
class Command(BaseCommand):
help = 'deletes expired events'
def handle(self, *args, **options):
today = datetime.datetime.now()
events = Event.objects.filter(date=datetime.date(2011,11,11))
for e in events:
e.delete()
self.stdout.write('Expired events successfully deleted.')
我正在运行的命令是:
$ python manage.py event_expiration
我已确保在管理和命令文件夹中添加 event_expiration.py 文件,并且这些文件夹具有 init 文件。这些在我的一个应用程序文件夹中。
我在这里忽略了什么吗?任何帮助表示赞赏,谢谢!
编辑:
SO用户Yuji帮助我尝试调试一下,但我们仍然很难过。这是我们所做的:
首先,完整的回溯和命令:
(venv)matt@inspirion14z:~/Dropbox/PROD/ersvp.it$ python manage.py event_expiration
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/matt/Dropbox/PROD/ersvp.it/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 70, in load_command_class
return module.Command()
AttributeError: 'module' object has no attribute 'Command'
要查看 django/core/management/ init .py" 的第 70 行发生了什么,我将 import pdb; pdb.set_trace() 放在文件中。
在调试模式下,我们尝试:
module.__file__
检查模块是否在预期的位置,并且确实如此,输出为:
'/home/matt/Dropbox/PROD/ersvp.it/app/management/commands/event_expiration.pyc'
接下来,我们尝试在 shell 中手动导入 Command:
>>> from app.management.commands.event_expiration import Command
Traceback (most recent call last): File "<console>", line 1, in <module> ImportError: cannot import name Command
还在挠头!