mibm
mibm

Reputation: 1368

Run some initialization code when Django (1.9) loads

I saw that from 1.7 there is https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready, however this is per each app.

What I am looking for is a way to call some initialization code when Django is loaded, regardless of which apps it contains. Many people suggest adding the code to wsgi.py, urls.py and settings.py, however all these methods are 'dirty'.

My scenario is that I have several apps that could be part of the Django deployment, and I would like any subset to have this init code. I could write a special common app, but I assume there must be a Django way of providing init code.

Upvotes: 0

Views: 348

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 602495

In newer versions of Django, you generally have a project directory containing the global, app-independent files like settings.py and wsgi.py. You can add global initialization code to __init__.py in that directory.

Be advised that this code is run very early (even before the settings are loaded), so some parts of the Django API are not available yet.

Upvotes: 1

Related Questions