How can I join two tables in Django and use the result in my template?
Question: I am having a hard time utilizing a foreign key in Django 1.7. For reference:

**models.py** looks like this:

    
    
    from django.db import models
    
    class Transaction(models.Model):
        transaction_num = models.IntegerField(primary_key=True)
        customer_name = models.CharField(max_length=255, blank=True)
        transaction_date = models.DateField(blank=True, null=True)
        phone = models.CharField(max_length=255, blank=True)
        est_pickup_date = models.DateField(blank=True, null=True)
    
        class Meta:
            db_table = 'transaction'
    
            def __str__(self):              # __unicode__ on Python 2
                return str(self.transaction_num)
    
    class Item(models.Model):
        itemid = models.IntegerField(primary_key=True)
        transaction_num = models.ForeignKey(Transaction, db_column='transaction_num')
        desc = models.CharField(max_length=255)
        picked_up_on = models.DateField(blank=True, null=True)
    
        class Meta:
            db_table = 'item'
    
        def __str__(self):              # __unicode__ on Python 2
            return str(self.itemid)
    
    class TransactionNote(models.Model):
        noteid = models.IntegerField(primary_key=True)
        transaction_num = models.ForeignKey(Transaction, db_column='transaction_num')
        content = models.CharField(max_length=255)
    
        class Meta:
            db_table = 'transaction_note'
    
        def __str__(self):              # __unicode__ on Python 2
            return str(self.noteid)
    

**views.py** looks like this:

    
    
    from django.http import HttpResponse
    from django.shortcuts import render
    from pickup.models import Transaction, Item, TransactionNote
    
    # Current Inventory
    def history(request):
        t = Transaction.objects.all().order_by('-transaction_date', '-transaction_num')
    
        return render(request, 'pickup/history.html', { 'transaction_list': t})
    

...and the relevant part of **history.html** , a table inside of a loop, where
I want to include the count of all of the related items attached to a
particular transaction:

    
    
    {% for transaction in transaction_list %}
    
    [...]
    
    <td>{{ transaction.customer_name }}</td>
    <td>{{ transaction.transaction_date }}</td>
    <td>{{ transaction.est_pickup_date }}</td>
    <td>{{ transaction.phone }}</td>
    <td>{{ transaction.itemid.count }}</td>
    
    [...]
    
    {% endfor %}
    

Everything works except for that last line. I have no idea how to join the
Transaction and Item tables so that I can work with the relevant item objects
(specifically the descriptions or their counts) inside of the template. Any
help will be appreciated.


Answer: Django provides the ability to query all reverse relationships for an object.

To achieve this, Django provides what is consider a 'related_name' for a
reverse relationship object, and it is defaulted to the class name with '_set'
appended.

So, your code should look something like this,

    
    
    {% for item in transaction.item_set.all %}
    ... do stuff ...
    {% endfor %}
    

or

    
    
    {{ transaction.item_set.count }}
    

Neither of these are tested, but I believe it should work closely to that.

