我正试图将django-celeron集成到现有网站中,但遇到了一个似乎无法修复的错误。
就上下文而言,我经历了Django的第一步,测试项目是成功的,即一切正常。
现在,在我现有的项目中,我无法让芹菜工人从命令行运行:
manage.py celery worker --loglevel=info --settings=myproject.settings.dev_settings
当我运行时,我得到以下堆栈跟踪和错误:
Traceback (most recent call last):
File "C:sitescorecrmmanage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 453, in execute_from_command_line
utility.execute()
File "C:Python27libsite-packagesdjangocoremanagement__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:Python27libsite-packagesdjcelerymanagementcommandscelery.py", line 22, in run_from_argv
['%s %s' % (argv[0], argv[1])] + argv[2:],
File "C:Python27libsite-packagescelerybincelery.py", line 901, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "C:Python27libsite-packagescelerybinbase.py", line 187, in execute_from_commandline
return self.handle_argv(prog_name, argv[1:])
File "C:Python27libsite-packagescelerybincelery.py", line 893, in handle_argv
return self.execute(command, argv)
File "C:Python27libsite-packagescelerybincelery.py", line 868, in execute
return cls(app=self.app).run_from_argv(self.prog_name, argv)
File "C:Python27libsite-packagescelerybincelery.py", line 148, in run_from_argv
return self(*args, **options)
File "C:Python27libsite-packagescelerybincelery.py", line 118, in __call__
ret = self.run(*args, **kwargs)
File "C:Python27libsite-packagescelerybincelery.py", line 220, in run
return self.target.run(*args, **kwargs)
File "C:Python27libsite-packagescelerybinceleryd.py", line 153, in run
return self.app.Worker(**kwargs).run()
File "C:Python27libsite-packagesceleryappsworker.py", line 162, in run
self.app.loader.init_worker()
File "C:Python27libsite-packagesceleryloadersbase.py", line 130, in init_worker
self.import_default_modules()
File "C:Python27libsite-packagesdjceleryloaders.py", line 138, in import_default_modules
self.autodiscover()
File "C:Python27libsite-packagesdjceleryloaders.py", line 141, in autodiscover
self.task_modules.update(mod.__name__ for mod in autodiscover() or ())
File "C:Python27libsite-packagesdjceleryloaders.py", line 176, in autodiscover
for app in settings.INSTALLED_APPS])
File "C:Python27libsite-packagesdjceleryloaders.py", line 195, in find_related_module
return importlib.import_module('%s.%s' % (app, related_name))
File "C:Python27libimportlib__init__.py", line 37, in import_module
__import__(name)
File "C:sitescorecrmpeopletasks.py", line 15, in <module>
from people.models import Customer, CustomerCsvFile, CustomerToTag, get_customer_from_csv_row
File "C:sitescorecrmpeoplemodels.py", line 163, in <module>
UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
File "C:Python27libsite-packagesdjangodbmodelsoptions.py", line 351, in get_field_by_name
cache = self.init_name_map()
File "C:Python27libsite-packagesdjangodbmodelsoptions.py", line 380, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "C:Python27libsite-packagesdjangodbmodelsoptions.py", line 469, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "C:Python27libsite-packagesdjangodbmodelsoptions.py", line 483, in _fill_related_many_to_many_cache
for klass in get_models(only_installed=False):
File "C:Python27libsite-packagesdjangodbmodelsloading.py", line 197, in get_models
self._populate()
File "C:Python27libsite-packagesdjangodbmodelsloading.py", line 75, in _populate
self.load_app(app_name)
File "C:Python27libsite-packagesdjangodbmodelsloading.py", line 96, in load_app
models = import_module('.models', app_name)
File "C:Python27libsite-packagesdjangoutilsimportlib.py", line 35, in import_module
__import__(name)
File "C:sitescorecrmbookingmodels.py", line 17, in <module>
from people.models import Customer, UserProfile
ImportError: cannot import name Customer
为了尝试弄清楚booking/models.py脚本在人们身上看到了什么,我在一开始就添加了以下内容:
import people
print 'path: %s' % people.__path__
for item in dir(people):
print item
这给了我以下输出:
path: ['C:\sites\corecrm\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
path: ['C:\sites\corecrm\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
但是,当我运行manage.py shell --settings=myproject.settings.dev_settings
时,我得到以下输出:
path: ['C:\sites\corecrm\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
path: ['C:\sites\corecrm\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
models
正如您所看到的,models
模块位于shell命令的第二个列表的末尾(我已经确认manage.py命令也是如此,而不是芹菜)。当我运行芹菜命令时,我如何确保该模块在同一点可用?
编辑:我现在也在Ubuntu虚拟机上设置了这个项目,当我试图运行worker-manage命令时,我也遇到了同样的错误。有什么想法吗?任何人
另一个版本:我已经将booking/models.py和people/models.by的代码粘贴在http://pastebin.com/fTVVBtB4
我很确定这一行是你的问题:
File "C:sitescorecrmpeoplemodels.py", line 163, in <module>
UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
当您仍然忙于从people.models
导入时,这一行(尤其是get_field_by_name
)迫使Django评估模型,并设置该模型与其相关模型之间的所有关系。这反过来又强制在people.models
中导入Customer
,而您仍忙于导入该精确模型。这就是ImportError
的结果。
对于一个有效的解决方案,你需要发布你的模型.py.
为什么这个错误只发生在芹菜上?如果没有更多的信息,我不能肯定,但我最好的猜测是Celery处理导入稍微不同的所有内容(Django可能不会同时导入Customer
、CustomerCsvFile
、CustomerToTag
和get_customer_from_csv_row
),这暴露了代码中的错误。
编辑/解决方案:
我会删除这行:
UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
并将其移动到实例级别,进入__init__
方法:
class UserProfile(AbstractUser):
def __init__(self, *args, **kwargs):
self._meta.get_field_by_name('username')[0]._max_length = 75
super(UserProfile, self).__init__(*args, **kwargs)
如果问题的原因确实是我认为的,那么这将修复循环导入,同时提供相同的功能。如果max_length功能以某种方式被破坏(很可能是因为内部向CharField添加了max_lengh验证器,并且_max_length更改得太晚),我会重写init方法中的完整username
字段:
class UserProfile(AbstractUser):
def __init__(self, *args, **kwargs):
super(UserProfile, self).__init__(*args, **kwargs)
self._meta.get_field_by_name('username')[0] = models.CharField(max_length=75, etc.)