显示多对多字段

发布于 2024-09-29 14:14:13 字数 1385 浏览 3 评论 0 原文

我似乎在显示工单时遇到问题。在我的应用程序中。客户没有同样的问题,为什么工单没有显示。实际上,它几乎就像出现了一个黑色空间,而不是应该从我的数据库中出现的文本。

问题似乎是因为工作订单具有多对多字段。如果我有 {{work_orders}} 而不是说 `{{work_orders.description}},我会

在 0xa042c6c> 处得到这个

这是我的应用程序的一些输出。

#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/>

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/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

只是我以为 2024-10-06 14:14:13

不应该是work_orders.all,例如

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

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

{% for work_order in work_orders.all %}
  {{work_order.description}}
{% endfor %}
英雄似剑 2024-10-06 14:14:13
#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 %}
#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 %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文