Django: How to disable Database status check at startup?

4k views Asked by At

As far as I know Django apps can't start if any of the databases set in the settings.py are down at the start of the application. Is there anyway to make Django "lazyload" the initial database connection?

I have two databases configured and one of them is a little unstable and sometimes it can be down for some seconds, but it's only used for some specific use cases of the application. As you can imagine I don't want that all the application can't start because of that. Is there any solution for that?

I'm using Django 1.6.11, and we also use Django South for database migrations (in case that it's related somehow).

2

There are 2 answers

0
Israel Fonseca On BEST ANSWER

I did some more tests and problem only happens when you are using de development server python manage.py runserver. In that case, it forces a connection with the database.

Using an actual WSGI server it doesn't happen as @Alasdair informed.

@JohnMoutafis in the end I didn't test your solution, but that could work.

0
John Moutafis On

I do not know how safe it is but you can do the following:

  1. In your settings.py start with empty DATABASES:

    DATABASES = {}
    
  2. In your apps.py, utilize the ready() hook to establish your database connections:

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

Now I cannot place my hand over fire for that one, but Django will try to reinitialize the database connection when needed.