Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#12661 closed (wontfix)

Support setting a "default" database

Reported by: kylef Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I have wrote two patches which allow you to select a "default" database, one is for a settings variable, another is a environmental variable. While I was playing around with multidb I found it annoying that I couldn't set a default db, I wanted to be able to use the same on my site for development and my production site.


os.environ['DJANGO_DEFAULT_DB'] = "development"

This could be set in your wsgi file.


DEFAULT_DB_ALIAS = "development"

This would go into

Attachments (2)

environ.diff (736 bytes) - added by kylef 6 years ago.
settings.diff (509 bytes) - added by kylef 6 years ago.

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by kylef

Changed 6 years ago by kylef

comment:1 Changed 6 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

You've been beaten to the punch by about 10 minutes :-)

[12272] introduced a way to define database routers, which can be used to solve the problem you describe.

comment:2 Changed 6 years ago by kylef

russellm, it doesn't quite allow it. Your default database still needs to work. I don't have MySQLdb on my development system, but that should not matter, I want to use a development database.

$ ./ syncdb --database=development
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
$ cat | head -n 26 | tail -n 2
    'development': {
        'ENGINE': 'django.db.backends.sqlite3',

If I was to do the same thing with a db router to make it use 'development' it would still fail because it is trying to load the 'default' database.

comment:3 Changed 6 years ago by russellm

I'm still not convinced. Even without using a router, you can solve this problem.

You've already said you're willing to accept an environment variable to control this. So why not just have three settings files - a common settings, production settings and development settings, and use DJANGO_SETTINGS_MODULE to switch between them.

If, for some reason, you don't want to use DJANGO_SETTINGS MODULE, you could still put the following in your settings:

import os
if os.environ.get('DJANGO_DEFAULT_DB'):
   DATABASES = {...}
   DATABASES = {...}

I don't see the advantage in introducing a new environment variable to allow the specific configuration pattern you describe.

comment:4 Changed 6 years ago by kylef

Hmm, ok, I think I will go with the if statement inside my settings module. I think I mis-understood what multidb support was for. Thanks for clearing this up.

Note: See TracTickets for help on using tickets.
Back to Top