首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django-suit admin for loop in tab

Django-suit admin for loop in tab
EN

Stack Overflow用户
提问于 2015-03-24 05:48:00
回答 1查看 643关注 0票数 0

我正在尝试创建一个带有django-suit扩展的选项卡,以查看报价并列出所有报价项目。我不太确定如何在django-suit扩展中做到这一点,因为我知道通常我会使用{% for items in x %}类型的模板代码。

如果任何人有使用此扩展并完成此任务的经验,或者如果我可以用其他方式完成此任务,使用original.pk传递主键将是非常好的。

models.py

代码语言:javascript
复制
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

代码语言:javascript
复制
<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 %}

EN

回答 1

Stack Overflow用户

发布于 2015-03-24 07:19:12

我必须用related_name更改models.py

代码语言:javascript
复制
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

代码语言:javascript
复制
{% 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 %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29221057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档