Minimial test of database connectivity in django -


i'm writing quick little test view can get every few minutes check application , database servers online , functioning.

from django.http import httpresponse, httpresponseservererror my_app.models import model  def test(request):     '''     returns httpresponse prove application/db servers work     '''     if request.method == "get":         #test database access         count = model.objects.values_list('id', flat=true).count()         if count:             return httpresponse("ok")      return httpresponseservererror("error") 

is there more minimal inbuilt database query test db server working , takling app server, without having fake 1 did above?

you can import django connections , check databases:

from django.db import connections try:     cursor = connections['default'].cursor() except operationalerror:     return httpresponseservererror("error") else:     return httpresponse("ok") return httpresponseservererror("error") 

documentation here


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -