无法在电子邮件中显示工单
您好,我正在尝试显示 mysql 数据库中的工单以显示在电子邮件中。但是,存在一个问题,因为 work_orders 是我的 Class Invoice Manytomany 字段的一部分。这给了我这个错误。
'ManyRelatedManager' object has no attribute 'description'
我不太确定问题是什么。以下是我的应用程序的一些可能有用的部分。
#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_list[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), '[email protected]', ['[email protected]'], 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}}
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_list[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), '[email protected]', ['[email protected]'], 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}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除非您向管理器添加了描述字段,否则该属性不存在(正如它所说)。
也许您想
在模板中使用 or
也许您需要在上下文中更改它
Unless you added a description field to the manager, the attribute doesn't exist (as it says).
Maybe you want to use
or in a template
And maybe you need to change it in the context
work_orders
不是列表。它是多对多领域的访问器/ORM 管理器。要获取实际的工作订单,您需要执行
work_orders.all()
(或.filter(foo=bar)
),然后迭代您返回的每个工作订单正确地格式化它们以包含在电子邮件中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