Skip to content

conf/patch.py causes circular imports with haystack with DEBUG=True #1194

Closed
ptoal opened this Issue Mar 10, 2012 · 8 comments

5 participants

@ptoal
ptoal commented Mar 10, 2012

The sekizai namespace checker calls Loader. If you have haystack installed, with django-cms, and a non-cms app that uses a PlaceholderField, BadThings(tm) happen. Eg:

    output = self.handle(*args, **options)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/core/management/commands/shell.py", line 47, in handle_noargs
    loaded_models = get_models()
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/db/models/loading.py", line 167, in get_models
    self._populate()
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django_openid/models.py", line 118, in <module>
    user_model = models.get_model('auth', 'User')
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/db/models/loading.py", line 190, in get_model
    self._populate()
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/ptoal/Development/bhyc_web/bhyc_pinax/apps/events/models.py", line 10, in <module>
    from cms.models.fields import PlaceholderField
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/src/django-cms/cms/__init__.py", line 9, in <module>
    patch_settings()
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/src/django-cms/cms/conf/__init__.py", line 32, in patch_settings
    post_patch_check()
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/src/django-cms/cms/conf/patch.py", line 50, in post_patch_check
    from django.template.loaders.app_directories import Loader
  File "/Users/ptoal/Development/virtualenvs/bhyc-dev/lib/python2.7/site-packages/django/template/loaders/app_directories.py", line 
23, in <module>
    raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name Event
@kezabelle

I'm piggybacking this somewhat, as I've seen the same traceback when trying to use Haystack, django CMS, and management commands that might import models from a module which also has CMSPlugin subclasses. I wasn't working on anything that could be described as a clean environment though, so it was nigh impossible to figure out if it was actually user error.

In my case, I don't believe I was using PlaceholderField at all, it was just the importing of anything from cms (eg: from cms.models.pluginmodel import CMSPlugin). This was whilst working against develop, about 2 months ago.

@ptoal
ptoal commented Mar 13, 2012

Yes, I expect that this is the same root cause. I just commented out everything after "check sekizai namespaces" in the conf/patch.py file.

This is ojii's code, so I'd like to hear his thoughts on fixing it.

@ojii
ojii commented May 22, 2012

I see that "check sekizai namespaces" is causing issues for people. the reason to include it was to prevent some obvious mistakes from happening. Maybe this shouldn't happen at all? Maybe those checks belong in a managament command (eg manage.py cms check)? I'm open for discussions, it seemed like a good idea at the time...

@kezabelle

Is this line from django.template.loaders.app_directories import Loader required to get Django's internal machinery 'ready' for validate_template to function correctly, then?

@kezabelle

Pinging regarding my previous comment, as I've encountered the problem again, and removing:

# check sekizai namespaces
    try:
        from pdb import set_trace; from pprint import pprint; set_trace()
        from django.template.loaders.app_directories import Loader
    except ImportError:
        return # south...

seems to 'fix' it, and I still can't figure out what that block is responsible for, as I still get

All templates defined in CMS_TEMPLATES must have at least the
'js' and 'css' sekizai namespaces.
The template 'whatever.html' does not.

correctly when those namespaces don't exist.

Addendum:

at least for the purposes of management commands, the fix seems to be simply to do an import from the cms before any of the custom models with placeholder fields etc.

from django.core.management.base import NoArgsCommand
from cms.models.pagemodel import Page # the important one. Anything that triggers post_patch_check, basically
from myapp.models import ThingWithPlaceholderDependency
@Larrik
Larrik commented Jul 16, 2012

kezabelle, that completely worked for me. You are a lifesaver!

@digi604
Divio AG member
digi604 commented Apr 8, 2013

Is this still the case with 2.4?

@digi604
Divio AG member
digi604 commented Jul 11, 2013

conf/patch.py is gone in 3.0... closing

@digi604 digi604 closed this Jul 11, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.