Django multiple settings files

I remember while working at my previous job as python developer, the question on how to organize Django Settings for multiple users used to come up often. The thing about using a good IDE (yaay pycharm) is that it helps you circumnavigate such trivial issues. But it's good to do this anyway.

So in order to put that matter to rest and for documentation purposes, there's a simple way to organize the settings so that they may work just fine in all environments (prod, dev etc.). For the purpose of this example I am assuming I have two, Production and Development.

First things first, rename the settings.py to base.py. We will make a new settings module that will contain base. So our hierarchy will look as follows:

MyApp
    MyApp                   #app with same name
        __init__.py
        urls.py
        wsgi.py
        settings            #a new settings module
            __init__.py
            base.py            #the old settings file
        manage.py

Now, simply add this to your init.py.

#__init__.py for our settings module
import os

ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  
from base import * # required because that is what we will override

# now first import dev
if os.path.isfile(os.path.join(ROOT_DIR, 'settings/dev.py')):  
    from dev import *
# and then import prod because that has to override all the settings
if os.path.isfile(os.path.join(ROOT_DIR, 'settings/prod.py')):  
    from prod import *

So, now if you have a dev it's gonna import that and override base. If it's not there, it won't bother importing it. On Prod, it's just gonna override everything since it is imported at the end. So, there we go. No issues.

Now I should point out that you won't need to have any of the files except base.py in the git tree. That way you don't have to have dev.py in version control. So now if you have multiple developers, they can all have a dev.py in their project tree.

Other people have also shared the way they do it, I haven't gone through all of them but I will if I encounter a problem with this method that I can't solve. Django Split Settings