How to display parent table columns in django template
<p>I have two models in django as below:</p>

<pre><code>class Directorates(models.Model):
  entrydate = models.DateTimeField(auto_now=True)
  directoratename = models.CharField("Directorate", max_length=1000)
  note = models.CharField("Note", max_length=2000, null=True)
  insertedby = models.IntegerField(null=False)
  updatedby = models.IntegerField(null=False, default='0')
  deletedby = models.IntegerField(null=False, default='0')

  def __str__(self):
    return self.directoratename


class Departments(models.Model):
  entrydate = models.DateTimeField(auto_now=True)
  departmentname = models.CharField("Department", max_length=1000)
  note = models.CharField("Note", max_length=2000, null=True)
  directorate = models.ForeignKey(Directorates, on_delete=models.CASCADE)
  insertedby = models.IntegerField(null=False)
  updatedby = models.IntegerField(null=False, default='0')
  deletedby = models.IntegerField(null=False, default='0')

  def __str__(self):
    return self.departmentname
</code></pre>

<p>What I want to do is to display id, departmentname, note from Departments model and directoratename from Directorates model in a table in template.</p>

<p>Below is my query which needs to be edited to meet the requirements.</p>

<pre><code> Departments.objects.filter(deletedby=0).values("id", "departmentname", "note", "directorate_id").order_by('-id')[:5]
</code></pre>

<p>Below is the code for table population.</p>

<pre><code>{% for d in data %}
  &lt;tr&gt;
    &lt;td&gt;{{ d.departmentname }}&lt;/td&gt;
    &lt;td&gt;directoratename to be displayed here.....&lt;/td&gt;
    &lt;td&gt;{{ d.note }}&lt;/td&gt;
    &lt;td class="pull-right"&gt;
      &lt;form action="{% url 'app:departments-delete' d.id %}" method="post"&gt;
        {% csrf_token %}
        &lt;div class="btn-group" role="group" aria-label="..."&gt;
          &lt;a href="{% url 'app:departments-edit' d.id %}" class="btn btn-warning glyphicon glyphicon-pencil"&gt;&lt;/a&gt;
          &lt;button type="submit" class="btn btn-danger glyphicon glyphicon-trash"&gt;&lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
{% endfor %}
</code></pre>

<p>I searched google to find the solution for this however I couldn't find any solution for this. Please help me.</p>

<p>You're making things harder for yourself by using a <code>values()</code> query. Just pass the objects themselves:</p>

<pre><code>departments = Departments.objects.filter(deletedby=0)
</code></pre>

<p>and now you can do:</p>

<pre><code>{% for d in data %}
  &lt;tr&gt;
    &lt;td&gt;{{ d.departmentname }}&lt;/td&gt;
    &lt;td&gt;{{ d.directorate.directoratename }}&lt;/td&gt;
</code></pre>

<p>Note that you can make this more efficient by using <code>select_related</code> in the original query:</p>

<pre><code>departments = Departments.objects.filter(deletedby=0).select_related('directorate')
</code></pre>

<p>Also note that it is normal in Django to give the models singular names - Department, Directorate - because an instance refers to a single one.</p>

python django django-models django-forms django-templates