Django DATABASE_ROUTERS don't work

12 views
Skip to first unread message

李华才

unread,
Jun 14, 2019, 7:15:51 AM6/14/19
to Django users
#  setting.py
DATABASES
= {
   
'remote': {
       
'ENGINE': 'django.db.backends.mysql',
       
'HOST': '10.0.3.57',
       
'NAME': 'fast_last',  
        'USER': 'faster',
        'PASSWORD': 'mypassword',  
        'TEST': {
           
'MIRROR': 'default',
        }
   
},
   
'default': {
       
'ENGINE': 'django.db.backends.mysql',
       
'NAME': 'fast',
        'USER': 'root',
        'PASSWORD': 'root',  
        'TEST': {
           
'NAME': 'test_fast_last',  
        }
   
}
}

DATABASE_ROUTERS = ['FasterRunner.database.AutoChoiceDataBase']

# database.py
import platform
class AutoChoiceDataBase:
    run_system
= platform.system()
    is_windows
= run_system == 'Windows'

    def db_for_read(self, model, **hints):
       
if self.is_windows:
           
return 'default'
        else:
           
return 'remote'

    def db_for_write(self, model, **hints):
       
if self.is_windows:
           
return 'default'
        else:
           
return 'remote'

when I push run this in Liunx server,it still use the default config.
python manage.py runserver 0.0.0.0:8000

  File "/home/faster/.virtualenvs/fasterenv/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")




Reply all
Reply to author
Forward
0 new messages