django-autoload
django-autoload is a reuseable django app which allows for correct initialization of your django project by ensuring the loading of signal handlers or indexes before any request is being processed.
Documentation
Installation
- Add django-autoload/autoload to settings.INSTALLED_APPS
INSTALLED_APPS = ( 'autoload', )
- Add the
autoload.middleware.AutoloadMiddleware
before any other middelware MIDDLEWARE_CLASSES = ('autoload.middleware.AutoloadMiddleware', ) + \ MIDDLEWARE_CLASSES
- Add the
How does django-autoload ensures correct initialization of my project?
django-autoload provides two mechanisms to allow for correct initializations:
- The
autoload.middleware.AutoloadMiddleware
middleware will load allmodels.py
from settings.INSTALLED_APPS as soon as the first request is being processed. This ensures the registration of signal handlers for example. - django-autoload provides a site configuration module loading mechanism. Therefore, you have to create a site configuration module. The module name has to be specified in your settings:
# settings.py:
AUTOLOAD_SITECONF = 'indexes'
Now, django-autoload will load the module indexes.py
before any request is being processed. An example module could look like this:
# indexes.py:
from dbindexer import autodiscover
autodiscover()
This will ensure the registration of all database indexes specified in your project.
autoload.autodiscover(module_name)
Some apps like django-dbindexer or nonrel-search provide an autodiscover-mechanism which tries to import index definitions from all apps in settings.INSTALLED_APPS. Since the autodiscover-mechanism is so common django-autoload provides an autodiscover
function for these apps.
autodiscover
will search for [module_name].py
in all settings.INSTALLED_APPS
and load them. django-dbindexer's autodiscover function just looks like this for example:
def autodiscover():
from autoload import autodiscover as auto_discover
auto_discover('dbindexes')