无法在电子邮件中显示工单

发布于 2024-09-30 02:34:48 字数 1597 浏览 6 评论 0原文

您好,我正在尝试显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2024-10-07 02:34:48

除非您向管理器添加了描述字段,否则该属性不存在(正如它所说)。
也许您想

for order in work_orders.all():
    print order.description

在模板中使用 or

{% for order in work_orders.all }}
    {{ order.description }}
{% endfor %}

也许您需要在上下文中更改它

'work_orders': work_orders

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
筱果果 2024-10-07 02:34:48

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文