[英]Django-suit admin for loop in tab
我正在嘗試使用django-suit擴展名創建一個選項卡,以查看報價並列出所有報價項目。 我不太確定如何在django-suit擴展程序中執行此操作,因為我幾乎會使用{% for items in x %}
類型的模板代碼。
如果有人對使用此擴展名和完成此任務有任何經驗,或者也許我可以通過其他方式做到這一點,則可以使用original.pk
傳遞主鍵,那將是不錯的選擇。
models.py
from django.db import models
class Quote(models.Model):
description = models.CharField(max_length=200)
date = models.DateField('Date Issued')
client = models.ForeignKey('organizations.Contact')
gst = models.BooleanField(default=True, verbose_name='GST Applicable')
assigned = models.BooleanField(default=True, verbose_name='Quote Assigned')
notes = models.TextField(blank=True)
def __str__(self):
return self.date.__str__().replace("-", "") + "-" + self.id.__str__().zfill(3)
class QuoteItem(models.Model):
order = models.PositiveIntegerField() # For Suit order sorting
invoice = models.ForeignKey(Quote)
description = models.CharField(max_length=200)
quantity = models.IntegerField(default=0)
price = models.DecimalField(max_digits=9, decimal_places=2)
def __str__(self):
return self.description
view_quote.html
<h2 class="legend">View Invoice
</h2><br>
{% if original.pk %}
<span>Invoice Information</span>
<table class="table-overview">
<tr>
<th>Invoice ID</th>
<td>
{{ original }}
</td>
</tr>
<tr>
<th>Description</th>
<td>
{{ original.description }}
</td>
</tr>
<tr>
<th>Client</th>
<td>
{{ original.client }}
</td>
</tr>
<tr>
<th>Notes</th>
<td>
{{ original.notes }}
</td>
</tr>
</table>
<span>Invoice Items</span>
<table class="table-overview">
<tr>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
{% for items in original.quoteitems %}
<tr>
<td>
{{ items.description }}
</td>
<td>
{{ items.price }}
</td> <td>
{{ items.quantity }}
</td> <td>
{{ items.total }}
</td>
</tr>
{% endfor %}
</table>
{% else %}
Item is not saved yet
{% endif %}
必須使用related_name更改models.py
class QuoteItem(models.Model):
order = models.PositiveIntegerField() # For Suit order sorting
quote = models.ForeignKey(Quote, related_name='quote_set') <<
description = models.CharField(max_length=200)
quantity = models.IntegerField(default=0)
price = models.DecimalField(max_digits=9, decimal_places=2)
和view_quote.html
{% for item in original.quote_set.all %}
<tr>
<td>
{{ item.description }}
</td>
<td>
{{ item.price }}
</td> <td>
{{ item.quantity }}
</td> <td>
{{ item.total }}
</td>
</tr>
{% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.