Django 内部結合 SQL views.py
from django.shortcuts import render,get_object_or_404, redirect
from django.http import HttpResponse
from django.views.generic import ListView
from django.db import connection
import psycopg2
# Create your views here.
#list
def index(request):
sqltext="""SELECT
a.id
, a.empid
, a.empname
, a.deptid
, a.mailaddress
, b.deptname
, b.upperdeptid
FROM
joinquery_employee a
INNER JOIN
joinquery_department b
on a.deptid=b.deptid
ORDER BY
a.id
; """
emplist=exec_query(sqltext)
return render(request, 'joinquery/index.html',{'emplist':emplist})
def exec_query(sqltext):
with connection.cursor() as cursor:
cursor.execute(sqltext)
""" 一行ずつ取得
get_arr =
for row in cursor:
get_arr.append(row)
print(get_arr[
0