单数据库

from django.db import connection

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

多数据库

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

参考连接:
直接执行自定义 SQL
将原始游标用于多个数据库


universe_king
3.4k 声望680 粉丝