【问题标题】:ValueError: The QuerySet value for an exact lookup must be limited to one result using slicingValueError:精确查找的 QuerySet 值必须使用切片限制为一个结果
【发布时间】:2021-10-20 12:50:31
【问题描述】:

我有一个models

class Product(models.Model):
    title = models.CharField(max_length=100)
    price = models.FloatField()
    category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
    slug = models.SlugField()
    description = models.TextField()
    image = models.ImageField()

class Order(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             on_delete=models.CASCADE, null=True)
    products = models.ManyToManyField(Products, through='OrderItem', related_name='orders')
    being_delivered = models.BooleanField(default=False)

class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='items')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='order_items')
    quantity = models.IntegerField(default=1)

我有views 用于创建OrderOrderItems 附加到Order

我有 views 用于购物车/订单详情:

我做什么:

from django.db.models import Sum, F

def cart_detail(request):
    order = Order.objects.filter(user=request.user)
    order_item = OrderItem.objects.filter(order=order).annotate(total=F('quantity') * F('product__price'))
    total = order_item.aggregate(total=Sum(F('quantity') * F('product__price')))['total']
    return render(request, 'cart/cart_detail.html', {'order':order, 'order_item':order_item, 'total':total}

模板:

<table class="table">
{% if order|length %}
    <thead>
    <tr class="alert alert-secondary" role="alert">
        <th>Photo</th>
        <th>Product</th>
        <th>Quantity</th>
        <th>Remove from cart</th>
        <th>Unit price</th>
        <th>Total price</th>
    </tr>
    </thead>

    <tbody>
    {% for item in order %}
    {% with product=item.product %}
    <tr>
        <td>
            <a href="{{ product.get_absolute_url }}">
                {% if product.photo %}
                <img src="{{ product.photo.url }}" class="img-fluid rounded-start" alt="..." width="250px">
                {% else %}
                <img src="https://globalimpactnetwork.org/wp-content/themes/globalimpact/images/no-image-found-360x250.png"
                     class="img-fluid rounded-start" alt="..." width="250px">
                {% endif %}</a>
        </td>
        <td>{{ product.title }}</td>
        <td>{{ order_item.quantity }}</td>
        <td><a href="" class="btn btn-outline-danger">Delete</a></td>
        <td class="num">$ {{  }} </td>
        <td class="num">$ {{  }} </td>
    </tr>
    {% endwith %}
    {% endfor %}
    <tr class="alert alert-success" role="alert">
        <td><b>Total price</b></td>
        <td colspan="4"></td>
        <td class="num"><b>$</b></td>
    </tr>
    </tbody>
</table>
<p class="text-right">
    <a href="{% url 'shop:home' %}" class="btn btn-secondary">Continue shopping</a>
    <a class="btn btn-warning" aria-current="page" href="{% url 'orders:order_create' %}">To order</a>
</p>

{% else %}
<div class="alert alert-primary" role="alert">
    <h3>Cart is empty</h3>
</div>

{% endif %}

{% endblock %}

我在 /cart/cart_detail 出现错误:ValueError。精确查找的 QuerySet 值必须使用切片限制为一个结果。

我在哪里做错了?

【问题讨论】:

  • 试试这个order_item = OrderItem.objects.filter(order_id__in=order)
  • @MojixCoder 是的,它正在工作!
  • @MojixCoder 你能帮我吗,total=字符串中的['total']是什么?
  • .aggregate() 返回具有该键的字典

标签: django django-views


【解决方案1】:

MojixCoder 帮我解决了我的问题。谢谢!

错误出现在filter(order=order),它可能对某人有用。

order_item = OrderItem.objects.filter(order_id__in=order)

【讨论】:

    猜你喜欢
    • 2020-11-13
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 2019-10-02
    • 2019-11-10
    • 2021-07-20
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多