使用 SQL 连接查询与 pyodbc mssql django

Using SQL join query with pyodbc mssql django

我正在尝试执行一些连接语句,但它们不起作用。

我看到的所有示例都是 php 管理员。我正在使用 SQL 服务器。我不想使用 django 模型进行连接,而是想使用查询来执行连接并将其呈现到我的 html 页面。

有人能告诉我怎么做吗?

这是我的代码:

models.py

from django.db import models


class Company(models.Model):
    name = models.CharField(max_length=100)
    contact_name = models.CharField(max_length=100)
    address = models.TextField(max_length=255)
    ph_no = models.CharField(max_length=17)
    tele = models.CharField(max_length=17)
    mail = models.EmailField(max_length=150)
    is_active = models.BinaryField(default=0)


class Client(models.Model):
    company_id = models.IntegerField()
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    ph_no = models.CharField(max_length=17)
    tele = models.CharField(max_length=17)
    mail = models.CharField(max_length=100)
    is_active = models.BinaryField(default=1)```



views.py


from django.shortcuts import render
from .models import Company, Client
import pyodbc
import datetime
import pytz

con = pyodbc.connect('Driver={SQL server};'
                     'Server=;'
                     'Database=Shipping;'
                     'Trusted_Connection=True;')
cursor = con.cursor()
con.autocommit = False

sql_join_client = '''
select Client.Client_id,Company.name,Client.first_name,Client.last_name,
Client.ph_no,Client.tele,Client.mail,Client.IsActive,Client.last_update
from Client
join Company on Client.company_id=Company.company_id
'''
#some names in model differ from the actual names in the db tables
def showclient(request):
    cursor.execute(sql_join_client)  # join
    result = cursor.fetchall()
    return render(request, 'client.html', {'Client': result})

client.html

{% extends 'base.html' %}
{% block table_name %}Client{% endblock %}
{% block table_view %}
<thead>
    <tr>
        <th>Client Id</th>
        <th>Company</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Ph. No.</th>
        <th>Telephone</th>
        <th>E-mail</th>
        <th>Is Active</th>
        <th>Last Updated</th>
        <th>Actions</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>Client Id</th>
        <th>Company</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Ph. No.</th>
        <th>Telephone</th>
        <th>E-mail</th>
        <th>Is Active</th>
        <th>Last Updated</th>
        <th>Actions</th>
    </tr>
</tfoot>
<tbody>
{% for datadisplay in Client%}
    <tr>
        <td>{{datadisplay.client_id}}</td>
        <td>{{datadisplay.company_name}}</td>
        <td>{{datadisplay.first_name}}</td>
        <td>{{datadisplay.last_name}}</td>
        <td>{{datadisplay.ph_no}}</td>
        <td>{{datadisplay.tele}}</td>
        <td>{{datadisplay.mail}}</td>
        <td>{{datadisplay.IsActive}}</td>
        <td>{{datadisplay.last_update}}</td>
        <td>
            <div class="d-grid gap-2">
                <a class="btn btn-warning btn-sm" href="/editclient/{{datadisplay.client_id}}">Edit</a>
                <a class="btn btn-danger btn-sm" href="/deleteclient/{{datadisplay.client_id}}">Delete</a>
            </div>
        </td>
    </tr>
{% endfor %}
</tbody>
<!--<center><a class="btn btn-primary" href="/addcompany">Add Record</a></center>-->
{% endblock %}
{% block add_record_btn %}
<a class="btn btn-primary" href="/addclient" style="margin-left:900px">Add Record</a>
{% endblock %}

所以这是我 get.i 缺少 client_id 和 company_name 的输出。

使用原始

def showclient(request):
    Client.objects.raw('ursqlcommand')

嘿@JuConte 我试过你说的但它给了我这个错误

第一个问题,您在 html 中传递“结果”,但迭代“客户端”, 其次,您可以使用 django 查询在视图中获得相同的结果

def showclient(request, pk): 
    company=get_object_or_404(Company, pk=pk)
    Client.objects.filter(company_id=company)
    return render(request, 'client.html', {'Client': result})

并在 html

{% extends 'base.html' %}
{% block table_name %}Client{% endblock %}
{% block table_view %}
<thead>
    <tr>
        <th>Client Id</th>
        <th>Company</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Ph. No.</th>
        <th>Telephone</th>
        <th>E-mail</th>
        <th>Is Active</th>
        <th>Last Updated</th>
        <th>Actions</th>
    </tr>
</thead>
<tfoot>
    <tr>
        <th>Client Id</th>
        <th>Company</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Ph. No.</th>
        <th>Telephone</th>
        <th>E-mail</th>
        <th>Is Active</th>
        <th>Last Updated</th>
        <th>Actions</th>
    </tr>
</tfoot>
<tbody>
{% for datadisplay in result %}
    <tr>
        <td>{{datadisplay.client_id}}</td>
        <td>{{datadisplay.company_name}}</td>
        <td>{{datadisplay.first_name}}</td>
        <td>{{datadisplay.last_name}}</td>
        <td>{{datadisplay.ph_no}}</td>
        <td>{{datadisplay.tele}}</td>
        <td>{{datadisplay.mail}}</td>
        <td>{{datadisplay.IsActive}}</td>
        <td>{{datadisplay.last_update}}</td>
        <td>
            <div class="d-grid gap-2">
                <a class="btn btn-warning btn-sm" href="/editclient/{{datadisplay.client_id}}">Edit</a>
                <a class="btn btn-danger btn-sm" href="/deleteclient/{{datadisplay.client_id}}">Delete</a>
            </div>
        </td>
    </tr>
{% endfor %}
</tbody>
<!--<center><a class="btn btn-primary" href="/addcompany">Add Record</a></center>-->
{% endblock %}
{% block add_record_btn %}
<a class="btn btn-primary" href="/addclient" style="margin-left:900px">Add Record</a>
{% endblock %}

检查是否有用