title: Traversing Django models downstream in template language
body: <p>I have the following model setup:</p>
  
  <pre><code>class Model1(models.Model):
      val1 = models.CharField(max_length=25, blank=True)
      val2 = models.CharField(max_length=25, blank=True)
      user = models.ForeignKey('users.User', on_delete=models.PROTECT, related_name='model1')
  
  class Model2(models.Model):
      val3 = models.BinaryField()
      model1_link = models.ForeignKey(Case, on_delete=models.CASCADE, related_name='model2')
  
  class Model3(models.Model):
      id = models.BigAutoField(primary_key=True)
      model2_link = models.ForeignKey(Model2, on_delete=models.CASCADE, related_name='model3')
  
  class Model4(models.Model):
      id = models.BigAutoField(primary_key=True)
      model3_link = models.ForeignKey(Model3, on_delete=models.CASCADE, related_name='model4', null=True, default=None)
      pred = models.CharField(max_length=50)
  </code></pre>
  
  <p>In my HTML template, I have a section where I iterate over entries from Model1 (e.g. val1), and would like to be able for each value, to include field 'pred' from Model4. Models 1-4 are daisy-chained through their FK`s at the moment. Yes, I know I could just include FK in Model4 linking it to Model1, but from logical point of view, I do not prefer this option at the moment.</p>
  
  <p>Anyway, expression like this does not get the job done on my end:</p>
  
  <pre><code>...
  {% for entry in model1_entries %}
  ...
      {% if user.is_superuser and entry.model2.model3.model4.count &gt; 0 %}
          something here
      {% endif %}
  ...
  {% endfor %}
  </code></pre>
  
  <p>I figure the problem has something to do with the fact that call model1.model2 returns a set of all model2's related to model1, but I dunno how I can pick one in this expression and run with it through the rest of models before reaching #4.</p>
  
  <p>Any ideas?</p>