0

我想将模型添加到命令中。/home/max/askmoiseev/ask/management/commands/cron.py

# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand, CommandError
from ask.models import Tag


class Command(BaseCommand):

    def handle(self, *args, **options):
        print args

$ python manage.py cron

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 75, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
  File "/home/max/askmoiseev/ask/management/commands/cron.py", line 4, in <module>
from ask.models import Tag
  File "/home/max/askmoiseev/ask/models.py", line 3, in <module>
from loginsys.models import User
  File "/home/max/askmoiseev/loginsys/__init__.py", line 3, in <module>
from ask.models import User
ImportError: cannot import name User

有什么问题?我试图这样做:

#!/bin/bash
export DJANGO_SETTINGS_MODULE=askmoiseev.settings
./manage.py cron

但这并没有帮助。

4

1 回答 1

2

下面应该工作。您必须确保 Python/Django 包 askmoiseev 是可导入的。要使其可导入,最简单的方法是将当前工作目录设置为包 askmoiseev 所在的目录。

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'askmoiseev.settings'
from ask.models import Tag
于 2014-06-01T09:36:07.160 回答