Search code examples
pythondjangopostgresqlconnectiondjango-1.8

How can I close a django 1.8 database cursor and connection?


Here is what I have:

from django.db import connection

class Command(BaseCommand):

option_list = BaseCommand.option_list

def handle(self, *labels, **options):
    with connection.cursor() as cursor:
        # Drop database 
        cursor.execute("drop database if exists test_db;")
        # Create database again
        cursor.execute("create database test_db;")

Where in this block can I close the db cursor and connection and what do I call to close them?


Solution

  • Found the solution.

    I close the connection out of the "with" block with connection.close() and that solves it.

    Thank you