Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created April 29, 2013 13:49
Show Gist options
  • Save kezabelle/5481668 to your computer and use it in GitHub Desktop.
Save kezabelle/5481668 to your computer and use it in GitHub Desktop.
Oh god why :(
app/docs/forms.rst:4: WARNING: autodoc: failed to import module u'app.forms'; the following exception was raised:
Traceback (most recent call last):
File "path/to/sphinx/ext/autodoc.py", line 326, in import_object
__import__(self.modname)
File "app/forms.py", line 8, in <module>
class MyAppForm(ModelForm):
File "path/to/django/forms/models.py", line 206, in __new__
opts.exclude, opts.widgets, formfield_callback)
File "path/to/django/forms/models.py", line 160, in fields_for_model
formfield = f.formfield(**kwargs)
File "path/to/django/db/models/fields/related.py", line 1031, in formfield
(self.name, self.rel.to))
ValueError: Cannot create form field for 'x' yet, because its related model u'otherapp.X' has not been loaded yet
@kezabelle
Copy link
Author

Horrid fix:

from django.conf import settings
from django.conf import global_settings as django_conf
packages = [ 
    '../..' # puts the entire project on the path (assuming apps are all contained within 1 directory)
]

here = os.path.dirname(__file__)
for pkg in packages:
    new_pkg = os.path.abspath(os.path.join(here, pkg))
    sys.path.insert(1, new_pkg)

django_conf.INSTALLED_APPS = ( 
    'app',
    'otherapp',
    'something_else',
)
settings.configure(default_settings=django_conf)
from django.db.models.loading import app_cache_ready, get_apps
if not app_cache_ready:
    get_apps()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment