'ValueError The QuerySet value for an exact lookup must be limited to one result using slicing on django views

am Getting this error i cant figure a way to solve it here are the 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 []

template 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 %}

and the 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>

if i change the transaction_list_view.html the include part to

{% include "sellers/transaction_list.html" with transaction_list=transactions %}

the error disappears but the transactions are not showing.



Solution 1:[1]

The accounts is a QuerySet, that means that it is a collection that contains zero, one or more SellerAccounts, so you should use:

products = Product.objects.filter(seller__in=account)

so with the __in lookup [Django-doc].

That being said, you can make the above query more effective by writing it as:

    def get_queryset(self):
        return Transaction.objects.filter(product__seller__user=self.request.user)

Here you thus will return the Transactions that have a product that have a seller that has as user the self.request.user.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1