示例:我有一个发票作为父模型,发票详细信息作为子模型。我想在发票模型管理中显示子详细信息作为发票条目。目标是在列表页面本身中实现综合视图。有没有其他方法可以实现此目的:它应该如下所示:
Invoice 1:
-details 1
-details 2
Invoice 2:
-details 1
-details 2
-details 3
Run Code Online (Sandbox Code Playgroud)
django 1.6.5 中有可用的模板吗?
假设以下示例:
模型.py
class Invoice(models.model):
date = models.DateTimeField() # for example
class InvoiceDetail(models.model):
invoice = models.ForeignKey(Invoice)
Run Code Online (Sandbox Code Playgroud)
视图.py
# example, don't fetch all in production
return render(request, 'mytemplate.html', {'invoices': Invoice.objects.all()})
Run Code Online (Sandbox Code Playgroud)
那么你的模板将是:
mytemplate.html
{% for invoice in invoices %}
<p>Invoice {{ invoice.id }} ({{ invoice.date }})
{% if invoice.invoicedetail_set %}
<ul>
{% for detail in invoice.invoicedetail_set %}
<li>Detail {{ detail.id }}</li>
{% endfor %}
</ul>
{% endif %}
</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
对于管理界面,Django 文档中有一个非常好的教程:教程:添加相关对象。
| 归档时间: |
|
| 查看次数: |
3626 次 |
| 最近记录: |