3

I am having trouble displaying fields of related tables in my template when using select_related()

Here is my model:

class Customer(models.model):
    customer_name = models.CharField(max_length=500)

class Orders(models.model):
    cust_id = models.ForeignKey(Customers)
    invoice_number = models.IntegerField()
    invoice_creation_date = models.DateTimeField('Invoice Created Date')

class Products(models.Model):                                                   
    name = models.CharField(max_length=500)                                     
    description = models.CharField(max_length=500)                              
    price = models.DecimalField(max_digits=20, decimal_places=2)

class Orders_Products(models.Model):                                            
    order_id = models.ForeignKey(Orders)                                        
    product_id = models.ForeignKey(Products)                                    
    quantity = models.IntegerField(default=0)

Here is my view:

def home(request):                                                              
    list_of_orders = Orders_Products.objects.select_related()
    template = 'erp_app/home.html'
    context = RequestContext(request, {'list_of_orders': list_of_orders})
    return render(request, template, context)

How do I represent related fields from Orders and Products, and especially Customers in a template. E.g. I want to display Orders.invoice_number, Products.name and Customer.customer_name from the same related record.

For example:

{% for order in list_of_orders %}
<tr>
    <td>{{ order.orders.invoice_number }}</td>
    <td>{{ order.products.name }}</td>
    <td>{{ order.customers.customer_name }}</td>
</tr>
{% endfor %}
Ben
  • 5,085
  • 9
  • 39
  • 58
  • Try `{{ order.product.name }}`. See also this [thread](http://stackoverflow.com/questions/19523698/django-select-related-in-template). – alecxe Jan 16 '14 at 03:14
  • Your suggestion didn't work. So he solved the problem by making the `select_related` part of the model. I'd like to keep it in the view, and send it to the template as the `context` of the `request`. – Ben Jan 16 '14 at 03:20
  • As @alecxe comment, your ForeignKey is called `product` not `products`. So you must use `order.product.name`. – slackmart Jan 16 '14 at 03:27
  • Ummm, isn't my ForeignKey called `product_id`, not `products`? @sgmart @alecxe – Ben Jan 16 '14 at 04:10
  • Thanks anyway, you both helped me figure it out. – Ben Jan 16 '14 at 04:14
  • Don't call your ForeignKey fields `foo_id`: they are not IDs, they are the actual related object. The underlying db field is an ID (and will be given a name with the `_id` appended), but the Django field is not. – Daniel Roseman Jan 16 '14 at 09:57
  • Thanks @DanielRoseman, I will incorporate that into my model. Appreciated =) – Ben Jan 16 '14 at 14:18

1 Answers1

7

I figured it out. I'm leaving this question and answer here for the next poor soul who has to nut his way through this conundrum.

{% for order in list_of_orders %}
<tr>
    <td>{{ order.order_id.cust_id.customer_name }}</td>
    <td>{{ order.order_id.invoice_number }}</td>
    <td>{{ order.order_id.invoice_creation_date }}</td>
    <td>{{ order.product_id.price }}</td>
</tr>
{% endfor %}
Ben
  • 5,085
  • 9
  • 39
  • 58