Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Delayed settings.DATABASES usage by ConnectionHandler

Refs #20474.
  • Loading branch information...
commit 2d8c132b187d9dfefe0f409c4fb39b54a6645675 1 parent 499a745
Claude Paroz authored May 21, 2013
7  django/db/__init__.py
@@ -2,7 +2,6 @@
2 2
 
3 3
 from django.conf import settings
4 4
 from django.core import signals
5  
-from django.core.exceptions import ImproperlyConfigured
6 5
 from django.db.utils import (DEFAULT_DB_ALIAS,
7 6
     DataError, OperationalError, IntegrityError, InternalError,
8 7
     ProgrammingError, NotSupportedError, DatabaseError,
@@ -13,11 +12,7 @@
13 12
 __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
14 13
     'IntegrityError', 'DEFAULT_DB_ALIAS')
15 14
 
16  
-
17  
-if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
18  
-    raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
19  
-
20  
-connections = ConnectionHandler(settings.DATABASES)
  15
+connections = ConnectionHandler()
21 16
 
22 17
 router = ConnectionRouter(settings.DATABASE_ROUTERS)
23 18
 
24  django/db/utils.py
@@ -6,6 +6,7 @@
6 6
 
7 7
 from django.conf import settings
8 8
 from django.core.exceptions import ImproperlyConfigured
  9
+from django.utils.functional import cached_property
9 10
 from django.utils.importlib import import_module
10 11
 from django.utils.module_loading import import_by_path
11 12
 from django.utils._os import upath
@@ -138,16 +139,27 @@ class ConnectionDoesNotExist(Exception):
138 139
 
139 140
 
140 141
 class ConnectionHandler(object):
141  
-    def __init__(self, databases):
142  
-        if not databases:
143  
-            self.databases = {
  142
+    def __init__(self, databases=None):
  143
+        """
  144
+        databases is an optional dictionary of database definitions (structured
  145
+        like settings.DATABASES).
  146
+        """
  147
+        self._databases = databases
  148
+        self._connections = local()
  149
+
  150
+    @cached_property
  151
+    def databases(self):
  152
+        if self._databases is None:
  153
+            self._databases = settings.DATABASES
  154
+        if self._databases == {}:
  155
+            self._databases = {
144 156
                 DEFAULT_DB_ALIAS: {
145 157
                     'ENGINE': 'django.db.backends.dummy',
146 158
                 },
147 159
             }
148  
-        else:
149  
-            self.databases = databases
150  
-        self._connections = local()
  160
+        if DEFAULT_DB_ALIAS not in self._databases:
  161
+            raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
  162
+        return self._databases
151 163
 
152 164
     def ensure_defaults(self, alias):
153 165
         """

0 notes on commit 2d8c132

Please sign in to comment.
Something went wrong with that request. Please try again.