Accessing multiple django models in template for loop
<p>I have looked everywhere for a solution to this particular problem, however I haven't found anything that answers this specific question.  Maybe I have it structured wrong but I'm here for help.  </p>

<p>My issue is I have a table that gets populated <a href="http://i.stack.imgur.com/prcW0.png" rel="nofollow">Table Image</a></p>

<p>In this table the first 4 columns come from my model AdmissionTypes and the last 2 columns are referencing some methods in my model Event.  This works fine as long as I only have one event in the system.  If I add more than one event, the numbers get combined.  I can't figure out a way to get all those items that are in each of those rows from one method.  I know I need to pass it a type_id for each admission_type that it iterates through, I'm just not sure how to do that since there's multiple type_ids that would get returned.  Here's the relevant code:</p>

<p>models.py</p>

<pre><code>class Event(models.Model): 
    name = models.CharField(max_length = 100)
    date = models.DateField()
    time = models.TimeField()
    cash = models.DecimalField(decimal_places = 2, max_digits = 10,)

    def tickets(self):
        return self.tickets_set.all()

    # This one works, but I use this for an ajax request
    def admission_type_count(self, type_id):
        return self.tickets_set.all().filter(type=type_id).aggregate(Count('type__type')).values()[0]

class AdmissionType(models.Model):
    event = models.ForeignKey(Event)
    type = models.CharField(max_length=100)
    price = models.DecimalField(decimal_places=2, max_digits=10)

    def admission_type(self):
        return self.tickets_set.all()

    def admission_type_count(self):
        return self.admission_type().aggregate(Count('type')).values()[0]

    def admission_type_total_price(self):
    return self.admission_type().aggregate(Sum(F('type__price'))).values()[0]
</code></pre>

<p>views.py</p>

<pre><code>def admission_types(request, event_id):
    tickets = Tickets.objects.filter(event=event_id)
    event = Event.objects.get(pk=event_id)
    types = event.admission_types().order_by('-price')
    expenses = event.expenses()
    total_revenue = event.tickets_total()
    cash = event.cash
    total_expenses = event.total_expenses()
    if total_revenue == None or total_expenses == None:
        cash_remaining = cash
    else:
        cash_remaining = ((total_revenue + cash) - total_expenses)
    for i in expenses:
        if i.percent &gt; 0:
            i.cost = total_revenue * i.percent/100
            i.save()

    return render(request, "admissions.html", locals())
</code></pre>

<p>admission.html
In this template, I'm trying to figure out how to loop through all the relevant data while still staying inside a table.  </p>

<pre><code>{% for i in types %}
    &lt;tr&gt;
        &lt;td&gt;{{ i.type }}&lt;/td&gt;
        &lt;td&gt;${{ i.price }}&lt;/td&gt;
        &lt;td class="add"&gt;
            &lt;button data-typeid="{{ i.id }}" id="add-{{ i.id }}"&gt;&lt;/button&gt;
        &lt;/td&gt;
        &lt;td class="del"&gt;
            &lt;button data-typeid="{{ i.id }}" id="delete-{{ i.id }}"&gt;&lt;/button&gt;
        &lt;/td&gt;
        &lt;td id="count-{{ i.id }}"&gt;{{ i.admission_type_count }}&lt;/td&gt;
        {% if request.user.is_superuser %}
            &lt;td id="total-{{ i.id }}"&gt;
                {% if i.admission_type_total_price == None %}
                    $0.00
                {% else %}
                    ${{ i.admission_type_total_price }}
                {% endif %}
            &lt;/td&gt;
        {% endif %}
    &lt;/tr&gt;
    {% endfor %}
</code></pre>

<p>Thanks for any help anyone can offer.  This is driving me nuts!</p>