cannot display work orders in an email
Hello I am trying to display work orders from my mysql database to show up in an email. However There is a problem because work_orders is a part of my Class Invoice manytomany field. This gives me this error.
'ManyRelatedManager' object has no attribute 'description'
I not really sure what the problem is. Here are some part of my app that could be helpful.
#views.py
@login_required
def invoice_mail(request, id=1):
invoices_list = Invoice.objects.filter(pk=id)
invoice = get_object_or_404(Invoice, pk=id)
client = invoices_li开发者_Python百科st[0].client
invoice_no = invoices_list[0].invoice_no
date = invoices_list[0].date
work_orders = invoices_list[0].work_orders
t = loader.get_template('registration/email.txt')
c = Context({
'client': client.company,
'address':client.address,
'city': client.city,
'postcode': client.postcode,
'email': client.email,
'date': date,
'invoice_no': invoice_no,
'work_orders': work_orders.description,
})
send_mail('Welcome to My Project', t.render(c), 'jess@example.com', ['tom@example.com'], fail_silently=False)
return render_to_response('sent_email.html', locals(), context_instance=RequestContext(request))
email.txt
INVOICE
Bill to: INVOICE # {{invoice_no}}
{{client}} DATE: {{date}}
{{address}}
{{city}}
{{postcode}}
{{email}}
quantity item Description
{{work_orders.description}}
Unless you added a description field to the manager, the attribute doesn't exist (as it says). Maybe you want to use
for order in work_orders.all():
print order.description
or in a template
{% for order in work_orders.all }}
{{ order.description }}
{% endfor %}
And maybe you need to change it in the context
'work_orders': work_orders
work_orders
is not a list. It's an accessor/ORM Manager for your many-to-many field.
To get the actual work orders, you need to do work_orders.all()
(or .filter(foo=bar)
) and then iterate over each work order you get back to format them decently for including in an email
精彩评论