samedi 30 mai 2015

Running django from a symlink directory so that __file__ reports symlink path rather than real path

I have a fairly basic django 1.8 site, the environment structure is as such:

/home/x/releases/v1
/home/x/releases/v2
/home/x/live-site -> /x/releases/v2
/home/x/database

My goal is that my sqlite DB config tells django that I want my database to be created in /home/x/database/blah.sqlite3.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, '../database/blah.sqlite3'),
    }
}

However, when running in the django shell (executed from /home/x/live-site) OR via apache with mod_wsgi (where there are NO references to /home/x/releases) __file__ is reported as /x/releases/v2/project/settings/PROD.py (I've rejigged my settings file structure from the default). Obviously that means when Django tries to construct the path to the DB it is ending up with /x/releases/v2/

Before I resort to something stupid like hardcoding the DB directory path as a config item, or accounting for my environment's deployment directory structure within my code...

Is there a way to prevent symlink paths being converted to their real path targets? Basically... I'm guessing some alternative to __file__? I've messed around with os.path.abspath and realpath, but can't seem to make any headway.

Aucun commentaire:

Enregistrer un commentaire