Single database

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute('SELECT version()')
    row = cursor.fetchone()

Multiple databases

from django.db import connections
with connections['my_db_alias'].cursor() as cursor:
    cursor.execute('SELECT version()')

Reference connection:
Execute custom SQL directly
uses original cursors for multiple databases


universe_king
3.4k 声望680 粉丝