Django display data in a table -- Sort of a table within a table (nested forloop?). Data needs to link as well
<p>I'm not sure the best way to explain it so I created an example picture and made up some data:</p>

<p><a href="http://i.stack.imgur.com/HclH8.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/HclH8.jpg" alt="Table"></a></p>

<p>I looked at this post and know I need to use some forloop template stuff: <a href="http://stackoverflow.com/questions/7287027/displaying-a-table-in-django-from-database">Displaying a Table in Django from Database</a>
<br></p>

<p>The parts that are throwing me off are how to have a table within a table. For example, the doctor 'Bob" has 3 different patients. Bob has one row but Bob's patient column has 3 rows within it. </p>

<pre><code>        &lt;table class="table table-striped table-bordered"&gt;
        &lt;thead&gt;
            &lt;th&gt;Name&lt;/th&gt;
            &lt;th&gt;Total Patient Meetings&lt;/th&gt;
            &lt;th&gt;Patient&lt;/th&gt;
            &lt;th&gt;Meetings for Patient&lt;/th&gt;
        &lt;/thread&gt;
        &lt;tbody&gt;
            {% for doctor in query_results %}
            &lt;tr&gt;
                &lt;td&gt; {{ doctor.name }} &lt;/td&gt;
                &lt;td&gt; {{ doctor.total_meetings }}&lt;/td&gt;

                //*Would I have a nested forloop in here?*//

                {% for patient in query_results2 %}
                &lt;td&gt; {{patient.name }} &lt;/td&gt;
                &lt;td&gt; {{patient.meeting_number }}&lt;/td&gt;
                {% endfor %}
            &lt;/tr&gt;
            {% endfor %}
        &lt;/tbody&gt;
    &lt;/table&gt;
</code></pre>

<p>I'm having trouble finding an example like this to work off of and am not sure how to approach it. </p>

<p>I also need those patients to link to the individual patient pages. So if I click on Bob's patient 'C755' I will be directed to that patient's page. I was thinking something like: </p>

<pre><code>   `&lt;p&gt;&lt;a href="patient/{{patient.name}}"&gt;{{patient.name}}:&lt;/a&gt;&lt;/p&gt;`
</code></pre>

<p>Thanks for any help. Just not sure the best way to approach this. </p>

<blockquote>
  <p>"A 'related manager' is a manager used in a one-to-many or many-to-many
  related context." 
  <a href="https://docs.djangoproject.com/en/1.10/ref/models/relations/" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/models/relations/</a></p>
</blockquote>

<p>I can't see how your relation between <code>doctor</code> and <code>patient</code> is, but supposing it is something like this:</p>

<pre><code>class Patient(models.Model):
    name = models.CharField(max_length=50)

class Doctor(models.Model):
    patient = models.ManyToManyField(Patient, verbose_name='patients')
</code></pre>

<p>You can easly iterate over <code>doctor.patient</code> using:</p>

<pre><code>{% for doctor in doctors %}
    {% for patient in doctor.patients.all %}
        {{ patient.name }}
    {% endfor %}
{% endfor %}
</code></pre>

<p>If you don't have a <code>verbose_name</code> attribute for the relation, then instead of using <code>doctor.patients.all</code> you use <code>doctor.patients_set.all</code>.</p>

<p>And to the patient's page, you can use a method inside your class <code>Patient</code> to easly do this:</p>

<pre><code>&lt;a href="{{ patient.get_absolute_url }}"&gt;{{ patient.name }}&lt;/a&gt;
</code></pre>

<blockquote>
  <p>"with get_absolute_url on your model it can make dealing with them on a per-object basis much simpler and cleaner across your entire site"</p>
</blockquote>

<p><a href="https://godjango.com/67-understanding-get_absolute_url/" rel="nofollow">https://godjango.com/67-understanding-get_absolute_url/</a></p>

python django