views:

19

answers:

2

Hello, I seem to have a problem displaying work orders. in my app. The clients does not have the same problem so why does the work orders not show up. Actually it is as almost as a black space appears rather than text that should appear from my database.

The problem seems to be because work orders have a many-to-many field. If I have {{work_orders}}instead of say `{{work_orders.description}}, I get this

<django.db.models.fields.related.ManyRelatedManager object at 0xa042c6c>

Here are some output from my app.

#views
@login_required 
def invoice_details(request, id=1):
    invoices_list = Invoice.objects.filter(pk=id) 
    client = invoices_list[0].client
    work_orders = invoices_list[0].work_orders
    return render_to_response(('invoice_details.html', locals()), {'work_orders': work_orders,'client': client, 'invoices_list': invoices_list}, context_instance=RequestContext(request))

#models.py
class Invoice(models.Model):
    client = models.ForeignKey(Client)
    date = models.DateField()
    invoice_no = models.CharField(max_length=16)
    work_orders = models.ManyToManyField(Work_Order)
    contract_info = models.ForeignKey(Contract_Info)

    def __unicode__(self):
                    return self.invoice_no

#invoice_details.html
   {{client.company}}<br/>
   {{client.address}}<br/>
   {{client.city}}<br/>
   {{client.postcode}}<br/>

   {{work_orders.description}}<br/>
   {{work_orders.quantity}}<br/>
   {{work_orders.item_no}}<br/>
+2  A: 

should it not be work_orders.all, e.g.

{% for work_order in work_orders.all %}
  {{work_order.description}}
{% endfor %}
Jameso
I get this "Caught TypeError while rendering: iteration over non-sequence", at "{% for work_order in work_orders %}".So that did not help.
Shehzad009
hi i think as edited by Daniel Roseman, if forgot to add my own suggestion! - should be work_orders.all
Jameso
A: 
#views
from django.shortcuts import get_object_or_404
@login_required 
def invoice_details(request, id=1):
    invoice = get_object_or_404(Invoice, pk=id) 
    return render_to_response(('invoice_details.html', locals()), {'invoice': invoice}, context_instance=RequestContext(request))

#models.py
class Invoice(models.Model):
    client = models.ForeignKey(Client)
    date = models.DateField()
    invoice_no = models.CharField(max_length=16)
    work_orders = models.ManyToManyField(Work_Order)
    contract_info = models.ForeignKey(Contract_Info)

    def __unicode__(self):
                    return self.invoice_no

#invoice_details.html
   {{ invoice.client.company }}<br/>
   {{ invoice.client.address }}<br/>
   {{ invoice.client.city }}<br/>
   {{ invoice.client.postcode }}<br/>

   {% for work_order in invoice.work_orders.all %}
     {{ work_order.description }}<br/>
     {{ work_order.quantity }}<br/>
     {{ work_order.item_no }}<br/>
   {% endfor %}
sunn0
This actually may work. Oh and its "from django.shortcuts import get_object_or_404"
Shehzad009
Sorry memory is good but short. Corrected the example.
sunn0