models.py
class Employee(models.Model):
emp_no = models.IntegerField(primary_key=True)
first_name = ...
last_name = ...
# emp_no first_name las
"select_related"
Returns a QuerySet that will “follow” foreign-key relationships.
from django.db import models
class City(models.Model):
# ...
pass
class Person(models.Model):
# ...
hometown = models.ForeignKey(
City,
on_delete=models.SET_NULL,
blank=True,
null=True,
)
class Book(models.Model):
# ...
author = models.ForeignKey(Person, on_delete=models.CASCADE)
then a call to Book.objects.select_related('author__hometown')
For your exmaples:
class EmpList(ListView):
queryset = DeptEmp.objects.all().select_related('emp_no').select_related('dept_no')
employee_list.html
{% for deptemp in object_list %}
{{ deptemp.emp_no.first_name }}
{{ deptemp.emp_no.last_name }}
{{ deptemp.dept_no.dept_name }}
{% endfor %}