File tree 1 file changed +8
-7
lines changed
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change 4
4
from django .core .exceptions import ImproperlyConfigured
5
5
from django .db .backends .base .base import BaseDatabaseWrapper
6
6
from django .utils .asyncio import async_unsafe
7
+ from django .utils .functional import cached_property
7
8
from pymongo .collection import Collection
8
9
from pymongo .driver_info import DriverInfo
9
10
from pymongo .mongo_client import MongoClient
@@ -149,13 +150,13 @@ def get_database(self):
149
150
return OperationDebugWrapper (self )
150
151
return self .database
151
152
152
- def __getattr__ ( self , attr ):
153
- """Connect to the database the first time `database` is accessed."""
154
- if attr == " database" :
155
- if self .connection is None :
156
- self .connect ()
157
- return getattr ( self , attr )
158
- raise AttributeError ( attr )
153
+ @ cached_property
154
+ def database ( self ):
155
+ """Connect to the database the first time it's accessed."""
156
+ if self .connection is None :
157
+ self .connect ()
158
+ # Cache the database attribute set by init_connection_state( )
159
+ return self . database
159
160
160
161
def init_connection_state (self ):
161
162
self .database = self .connection [self .settings_dict ["NAME" ]]
You can’t perform that action at this time.
0 commit comments