ValueError: Значение QuerySet для точного поиска должно быть ограничено одним результатом с помощью нарезки
У меня есть 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
для создания Order
и OrderItems
, прикрепленных к Order
.
У меня есть views
для cart/order_detail:
что я делаю:
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 %}
И у меня есть Error: ValueError at /cart/cart_detail. Значение QuerySet для точного поиска должно быть ограничено одним результатом с помощью нарезки.
Что я делаю неправильно и где?
MojixCoder Помог мне с моей проблемой. Спасибо!
Ошибка была в filter(order=order)
, может кому-то пригодится.
order_item = OrderItem.objects.filter(order_id__in=order)