I am trying to perform some join statements but they are not working.
All the examples I see are with php admin. I am using SQL Server. I don't want do do the joins using the the django models, instead I want to use queries to perform the join and render it to my html page.
Please can someone tell me how to do this?
Here is my code:
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 %}
so this is the out put i get.i m missing the client_id and company_name.
use raw
def showclient(request):
Client.objects.raw('ursqlcommand')