Django: How to disable checking the status of the database on startup?

As far as I know, Django apps cannot start if any of the databases installed in settings.py

are at the beginning of the app. Is there anyway to make Django "lazyload" the original database connection?

I have two databases configured, and one of them is a bit unstable and sometimes it might not be available for a few seconds, but it is only used for some specific use cases of the application. As you can imagine, I don't want the whole application to be unable to start because of this. Is there any solution for this?

I am using Django 1.6.11 and we are also using Django South for database migrations (in case it is related in one way or another).

+3


source to share


2 answers


I did some more tests and the problem only occurs when using the development server python manage.py runserver

. In this case, it forces a connection to the database.

Using a real WSGI server is not happening as reported by @Alasdair.



@JohnMoutafis, in the end I haven't tested your solution, but it might have worked.

+1


source


I don't know how safe it is, but you can do the following:

  • The settings.py

    start from scratch DATABASES

    :

    DATABASES = {}
    
          

  • In yours, apps.py

    use a hook ready()

    to establish database connections:

    from settings.py import DATABASES
    
    class YourAppConfig(AppConfig):
    
        def ready(self):
            DATABASES.update({
                Your database connections
            })
    
          



Now I can't put my hand behind the fire for this, but Django will try to re-initialize the database connection when needed.

+1


source







All Articles