Re-organizing my Django repository

Recently I did some significant reorganization of my Django applications and projects in order to more fully decouple them.  Django is clearly intended to operate this way, but as James Bennett acknowledges in a post to Django users, the Django tutorial puts apps inside the project for simplicity, and I had followed this pattern into production.  I took his advice in that same post to move my apps to top-level Python packages and out of the project directory. This was my initial setup under /opt/django:

apps/ (Project)
    __init__.py
    admin.py
    settings.py
    urls.py
    manage.py
    myapp1/
        __init__.py
        admin.py
        models.py
        views.py
        urls.py
    myapps2/
        ...

The project directory was a top-level Python package, so my Apache/mod_python configuration had this:

PythonPath "['/opt/django'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE apps.settings

and my application modules were accessed via the project path. This presented no functional problems at the time since I had only one project. However, I did have multiple installations of Django for which I wanted different settings, URLs, and admin UIs. I should have treated these as separate projects, but I got around that by re-using the same project in different ways for each installation.  But I really wanted to keep all the source under version control and in a single tree (I was keeping admin.py, settings.py and urls.py out of the repository so they could be different for each installation), and it just bothered me to have project-path-dependent code.

After reorganizing, my repository looks like this:

apps/
    myapp1/
        __init__.py
        admin.py
        models.py
        views.py
        urls.py
    myapps2/
        ...
projects/
    myproject1/
        __init__.py
        settings.py
        admin.py
        urls.py
        manage.py
    myproject2/
        ...

and my Apache/mod_python conf:

PythonPath "['/opt/django', '/opt/django/apps', '/opt/django/projects'] + sys.path"
SetEnv DJANGO_SETTINGS_MODULE myproject1.settings

(I kept /opt/django on the PythonPath because there are some common packages there.)

Advertisements

, ,

  1. #1 by David Chandek-Stark on January 6, 2009 - 11:44 am

    As a followup, I strongly recommend Malcolm Tredinnick’s post on developing without projects: http://www.pointy-stick.com/blog/2007/11/09/django-tip-developing-without-projects/. This content should be considered for inclusion in the official Django documentation. The Django docs are quite good — stellar, really — but could use some supplemental “advice” to help folks navigate through some of the decision points.