title: Reverse relation of foreign keys in django
body: <p>I have two tables connected via foreign keys.</p>
  
  <p>Models.py</p>
  
  <pre><code>class Foo(models.Model):
      A = models.DateField(default=datetime.now)
      B = models.IntegerField(default=0)
      C = models.IntegerField(default=0)
  
  class Bar(models.Model):
      X = models.ForeignKey(Foo, on_delete=models.CASCADE, related_name='foo_a')
      Y = models.CharField(default=0)
      Z = models.CharField(default=0)
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>def foo_details(request):
  
      query = Foo.objects.all()
  
      return render(request, 'foodetails.html', {'query': inward})
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>    &lt;thead&gt;
      &lt;tr&gt;
          &lt;th&gt;A&lt;/th&gt;
          &lt;th&gt;B&lt;/th&gt;
          &lt;th&gt;C&lt;/th&gt;
          &lt;th&gt;Y&lt;/th&gt;
          &lt;th&gt;Z&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          {% for i in query %}
  
             &lt;tr&gt;
              &lt;td class="align-middle"&gt;{{ i.A }}&lt;/td&gt;
              &lt;td class="align-middle"&gt;{{ i.B }}&lt;/td&gt;
              &lt;td class="align-middle"&gt;{{ i.C }}&lt;/td&gt;
              &lt;td class="align-middle"&gt;{{ i.?? }}&lt;/td&gt;
              &lt;td class="align-middle"&gt;{{ i.?? }}&lt;/td&gt;
  
             &lt;/tr&gt;
  </code></pre>
  
  <p>How can I show the related field of Bar model in my template? </p>
  
  <p>If the query in my views was <code>Bar.objects.all()</code> then in my templates I could easily do </p>
  
  <pre><code>&lt;td class="align-middle"&gt;{{ i.X.A }}&lt;/td&gt;
  </code></pre>
  
  <p>But how can I use the reverse relation? </p>