Make template of a directly raw query and return results in table

0

Hello, I'm new to django and I'm using a read-only database and I just want to extract some data and table display in hmtl, but I'm not able to return column by column inside the table, help me! I'm using a directly raw query!

Model.py

from django.db import connection


# Create your models here.
def dictfetchall(cursor):
    "Returns all rows from a cursor as a dict"
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
]


def my_custom_sql(self):
    with connection.cursor()as cursor:
        cursor.execute("""
                       SELECT EQUIP_ID, LINE_CODE, PLANT_CODE 
                       FROM tbs_rm_mnt_shift_sumr 
                       where SUMR_YMD = '20180405' AND SIDE_CODE = 'T' AND 
                       rownum < 20
                       """ )
        row = dictfetchall(cursor)
        return row

view.py

from django.shortcuts import render, redirect, get_object_or_404
from .models import my_custom_sql


# Create your views here.

def show_list(request):
    query= my_custom_sql(self='my_custom_sql')
    return render(request,'monitoring.html', {'query': query})

monitoring.html

<table border="2" style="solid black">
     <tr>
        <td>Equip</td>
        <td>Line</td>
        <td>Plant</td>

        {% for instance in query %}
            {% for field, value in instance.items %}

                <tr>
                    <td>{{ value }} </td>

                </tr>
            {%endfor%}
        {% endfor %}
    </tr>
</table>

output:

    
asked by anonymous 10.04.2018 / 15:51

0 answers