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.AutoloadMiddlewarebefore any other middelware MIDDLEWARE_CLASSES = ('autoload.middleware.AutoloadMiddleware', ) + \ MIDDLEWARE_CLASSESHow does django-autoload ensures correct initialization of my project?
django-autoload provides two mechanisms to allow for correct initializations:
- The
autoload.middleware.AutoloadMiddlewaremiddleware will load allmodels.pyfrom 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.pybefore 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
autodiscoverfunction for these apps.autodiscoverwill search for[module_name].pyin allsettings.INSTALLED_APPSand 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')- The
