有交易的明细视图,交易与销售是一对多的关系。我想在详细视图中显示所有带有交易的销售额

Have detail view for transaction, transaction has a one to many relationship with sale. I want to display all sales with transaction in detail view

我正在尝试在 Django 的详细视图中显示关系的多个方面。一次交易可以有多次销售。

我的观点如下:

class 事务详细视图(详细视图): 型号 = 交易

URL 下面: 路径('transaction//',TransactionDetailView.as_view(),名称='transaction-detail'),

HTML 下面:

<h1> Transaction</h1>
<br/>
<div class = "row">
    <div class = "col-md-12">
        <h4>Purchased Coins</h4>

        <table class="table">
            <thead>
                 <tr>
                     <th scope="col">Coin</th>
                     <th scope="col">Amount</th>
                     <th scope="col">per Coin</th>
                     <th scope="col">Total</th>
                     <th scope="col">Value Per Coin</th>
                     <th scope="col">Total Value</th>
                     <th scope="col">Date Added</th>
                     <th scope="col">Profit/Loss</th>
                     <th scope="col">%Profit/Loss</th>
                 </tr>
            </thead>
            <tbody>

             <tr>
                 <td>{{object.currency}}</td>
                 <td>{{object.amount}}</td>
                 <td>{{object.amount_per_coin}}</td>
                 <td>{{object.total_price}}</td>
                 <td><p>Get From API</p></td>
                 <td><p>{{object.amount}} * Get From API</p></td>
                 <td>{{object.date_purchased|date:"F d, Y"}}</td>
                 <td><p>TBC</p></td>
                 <td><p>TBC</p></td>
             </tr>

             </tbody>
        </table>

正在尝试创建与交易关联的销售列表

        {% for sale in object.sales %}
        <p>kkk</p>
        {% endfor %}

你想要的是使用 SingleObjectMixin with a ListView,使用 SingleObjectMixin 提供 transaction 和 ListView 列出所有相关的 sales