1👍
✅
You can register a signal on class-prepared.
https://docs.djangoproject.com/en/dev/ref/signals/#class-prepared
Than try executing custom sql directly.
https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly
If it fails raise your custom exception.
0👍
import time
from django.db import connections
from django.db.utils import OperationalError
self.stdout.write('Waiting for database...')
db_conn = None
while not db_conn:
try:
db_conn = connections['default']
except OperationalError:
self.stdout.write('Database unavailable, waiting 1 second...')
time.sleep(1)
self.stdout.write(self.style.SUCCESS('Database available!'))
you can use this snippet where you need to.
befor accessing database and making any queries you must check if the database is up or not
- [Answer]-How to insert images in TextField from Django admin?
- [Answer]-Django-userena: how to activate account for local development?
- [Answer]-Django AbstractUser Inheritence Error
- [Answer]-Ajax to notify about variable change on server
- [Answer]-Optimising select_related fields for auth user profile
Source:stackexchange.com