我收到此错误,我想不出一种解决方法,这里是views.py
class SellerTransactionListView(ListView):
model = Transaction
template_name = "sellers/transaction_list_view.html"
def get_queryset(self):
account = SellerAccount.objects.filter(user=self.request.user)
if account.exists():
products = Product.objects.filter(seller=account)
return Transaction.objects.filter(product__in=products)
return []
模板transaction_list_view.html
{% extends "base.html" %}
{% block content %}
<h1>Transactions</h1>
<ul>
{% include "sellers/transaction_list.html" with transaction_list=object_list %}
</ul>
{% endblock %}
和transaction_list.html
<table>
<thead>
<th>Product</th>
<th>User</th>
<th>order_id</th>
<th>Sale Total</th>
<th></th>
</thead>
<tbody>
{% for trans in transaction_list %}
<tr>
<td>{{ trans.product }}</td>
<td>{{ trans.profile }}</td>
<td>{{ trans.order_id }}</td>
<td>{{ trans.amount }}</td>
<td>{{ trans.timestamp|timesince }} ago</td>
</tr>
{% endfor %}
</tbody>
</table>
如果我将transaction_list_view.html包含部分更改为
{%包含“ sellers / transaction_list.html”和transaction_list = transactions%}
错误消失,但交易未显示。
该account
s是QuerySet
,这意味着它是一个包含零,一个或多个集合SellerAccount
S,所以你应该使用:
products = Product.objects.filter(seller__in=account)
因此使用__in
查找[Django-doc]。
话虽如此,您可以通过将其编写为以下命令来使上述查询更有效:
def get_queryset(self):
return Transaction.objects.filter(product__seller__user=self.request.user)
因此,您将在此处返回Transaction
具有的的product
,seller
其的拥有user
的self.request.user
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句