调用raw方法写原生sql语句
def show_index(request):
queryset = Subject.objects.raw('select no, name, intro from tb_subject where is_hot=1')
return redirect('/static/html/subjects.html')
使用get_connectio方法绕过Django的ORM机制(纯原生)
from django.db.transaction import get_connection
def show_index(request):
try:
conn = get_connection(using='default')
with conn.cursor() as cursor:
cursor.execute('select name from tb_subject where is_hot=1')
print(cursor.fetchall())
finally:
conn.close
return redirect('/static/html/subjects.html')