0

我有一个自定义类别模型扩展了CategoryBasedjango-categories。

当我将它放入干草堆索引并重新启动服务器时,在第一次运行时它会抱怨cannot import name CategoryBase无法CategoryBase从中导入,categories.base但是如果我再次刷新页面,该站点运行正常并且搜索结果返回正确的信息。这似乎是因为进口的顺序。

我查看了堆栈跟踪,发现了源自admin.autodiscoverurls.py 内部的错误,导入在 urls.py 中看起来像这样

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()           
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic import TemplateView
from django.contrib.sitemaps import GenericSitemap
from galleries.models import Gallery 
from events.models import Event
from django.contrib.auth.views import login, logout
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
from core.models import StaticPageSitemap
from articles.models import ArticleSitemap, IssueSitemap
dajaxice_autodiscover()   

我尝试将自动发现移动到所有导入之后,如下所示:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic import TemplateView
from django.contrib.sitemaps import GenericSitemap
from galleries.models import Gallery 
from events.models import Event
from django.contrib.auth.views import login, logout
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
from core.models import StaticPageSitemap
from articles.models import ArticleSitemap, IssueSitemap
admin.autodiscover()
dajaxice_autodiscover()

虽然错误在第一次运行时消失了,但搜索没有返回任何类别的结果。

这是堆栈跟踪:

Django Version: 1.5
Python Version: 2.7.3
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.comments',
 'django.contrib.sites',
 'django.contrib.markup',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sitemaps',
 'django.contrib.sessions',
 'django.contrib.admin',
 'djcelery',
 'filebrowser',
 'compressor',
 'south',
 'taggit',
 'categories',
 'categories.editor',
 'core',
 'articles',
 'newsletters',
 'galleries',
 'events',
 'haystack',
 'dajaxice',
 'zencode',
 'videos',
 'directory',
 'dps_payment',
 'home',
 'taggit_autosuggest',
 'captcha',
 'search',
 'media_board']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  92.                     response = middleware_method(request)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/middleware/common.py" in process_request
  69.             if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in is_valid_path
  551.         resolve(path, urlconf)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  440.     return get_resolver(urlconf).resolve(path)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/srv/www/weddings/weddings_advanced/weddings/urls.py" in <module>
  3. admin.autodiscover()
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py" in autodiscover
  29.             import_module('%s.admin' % app)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/auth/admin.py" in <module>
  172. admin.site.register(Group, GroupAdmin)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in register
  98.                 validate(admin_class, model)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/contrib/admin/validation.py" in validate
  22.     models.get_apps()
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py" in get_apps
  134.         self._populate()
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py" in _populate
  75.                     self.load_app(app_name)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/db/models/loading.py" in load_app
  96.             models = import_module('.models', app_name)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/srv/www/weddings/weddings_advanced/videos/models.py" in <module>
  9. from categories.models import Category
File "/srv/www/weddings/local/lib/python2.7/site-packages/categories/models.py" in <module>
  13. from .base import CategoryBase
File "/srv/www/weddings/local/lib/python2.7/site-packages/categories/base.py" in <module>
  32. class CategoryBase(MPTTModel):
File "/srv/www/weddings/local/lib/python2.7/site-packages/categories/base.py" in CategoryBase
  41.         verbose_name=_('parent'))
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/translation/__init__.py" in ugettext
  71.     return _trans.ugettext(message)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in ugettext
  276.         return do_translate(message, 'ugettext')
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in do_translate
  258.             _default = translation(settings.LANGUAGE_CODE)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in translation
  172.     default_translation = _fetch(settings.LANGUAGE_CODE)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py" in _fetch
  154.             app = import_module(appname)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/srv/www/weddings/local/lib/python2.7/site-packages/haystack/__init__.py" in <module>
  154. handle_registrations()
File "/srv/www/weddings/local/lib/python2.7/site-packages/haystack/__init__.py" in handle_registrations
  151.     search_sites_conf = importlib.import_module(settings.HAYSTACK_SITECONF)
File "/srv/www/weddings/local/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/srv/www/weddings/weddings_advanced/search/search_sites.py" in <module>
  8. from directory.models import Listing, Category as DirectoryCategory
File "/srv/www/weddings/weddings_advanced/directory/models.py" in <module>
  21. from categories.base import CategoryBase

Exception Type: ImportError at /directory/listing/new-listing-gift
Exception Value: cannot import name CategoryBase
4

1 回答 1

0

Finally managed to fix it!!!!

Root cause was in from categories.models import Category in videos app, which is another app model importing the Category which extends CategoryBase, and it's causing circular reference, to fix it, in the models.py of videos app, change from direct import to lazy import as below:

categories = models.ManyToManyField('categories.Category', null=True, blank=True)

Update:

Above fix only worked very briefly and then I got other circular import problems on other models, what finally fixed it was upgrading haystack to v2.1.0

于 2013-08-15T23:21:52.420 回答