如果不想定义model,直接执行自定义SQL,可如下操作:

1. 通过 connections获取db连接,如果是多个数据库,connections['dbName'] 来选择

2. 获取游标 cursor

3. 执行sql: cursor.execute(sql)

4.获取返回结果:fetchone,fetchall (fetchall返回的是元祖,非字典)

from django.db import connections
cursor = connections['test_db'].cursor()
cursor.execute('SELECT * FROM ...')
# fetchall返回的是元祖...
users = cursor.fetchall()

 

参考:

https://blog.csdn.net/iloveyin/article/details/46380619

https://blog.csdn.net/qq_37099834/article/details/82754170

相关文章:

  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-20
  • 2021-12-04
  • 2022-01-19
猜你喜欢
  • 2021-06-17
  • 2021-12-10
  • 2022-12-23
  • 2021-09-18
  • 2022-12-23
  • 2021-11-11
  • 2021-07-06
相关资源
相似解决方案