adding item to cart when click on the button it show this error
Error:
ValueError at /baskets/add-to-basket/action-chair-c4-35x35-951753
Cannot use QuerySet for "Product": Use a QuerySet for "Item".
Also the Error Highlited this line:
order_item, created = OrderItem.objects.get_or_create(item=item, account=request.user, ordered=False)
When i Change item = Product.objects.filter(slug=slug)
into item = Item.objects.filter(slug=slug)
, it shows error for the slug not an attribute of Object Item.
HTML Button
<div class="extra content">
<a class="ui fluid button" href="{{ item.get_add_to_basket_url }}">Add To Basket</a>
<div class="ui divider"></div>
<a class="ui fluid button" href="{{ item.get_remove_from_basket_url }}">Remove From Basket</a>
</div>
Views.py
def ItemAddToBasketView(request, slug):
item = Product.objects.filter(slug=slug)
order_item, created = OrderItem.objects.get_or_create(item=item, account=request.user, ordered=False)
order_qs = Order.objects.filter(account=request.user, ordered=False)
if order_qs.exists():
order = order_qs[0]
if order.items.filter(item__slug=item.slug).exists():
order_item.quantity += 1
order_item.save()
message.success(request, f'{item} added Quantity is Updated in Basket.')
return redirect('Baskets:BasketDetailsUrl')
else:
order.items.add(order_item)
order.save()
message.success(request, f'{item} added to Basket.')
return redirect('Baskets:BasketDetailsUrl')
else:
order = Order.objects.create(account=request.user, ordered=False, ordered_at=ordered_at)
order.items.add(order_item)
order.save()
return redirect('Baskets:BasketDetailsUrl')
Models.py
class Item(models.Model):
product = models.OneToOneField(Product, verbose_name=_('Product'), on_delete=models.CASCADE)
def __str__(self):
return f'{self.product}'
class OrderItem(models.Model):
account = models.ForeignKey(Account, verbose_name=_('Order Account'), on_delete=models.CASCADE)
item = models.ForeignKey(Item, verbose_name=_('Item'), on_delete=models.CASCADE)
ordered = models.BooleanField(verbose_name=_('Ordered'), default=False)
quantity = models.IntegerField(verbose_name=_('Quantity'), default=1)
def __str__(self):
return f'{self.item} - {self.quantity} Pieces.'
class Order(models.Model):
order_id = models.UUIDField(verbose_name=_('Order Number'), primary_key=True, default=uuid4, unique=True, editable=False)
account = models.ForeignKey(Account, verbose_name=_('Order Account'), on_delete=models.CASCADE)
items = models.ManyToManyField(OrderItem, verbose_name=_('Items'))
ordered = models.BooleanField(verbose_name=_('Ordered'), default=False)
created_at = models.DateTimeField(verbose_name=_('Created At'), auto_now_add=True, editable=False)
ordered_at = models.DateTimeField(verbose_name=_('Ordered At'), auto_now=True)
def __str__(self):
return f'Order Id: {self.order_id} - {self.account}.'
Grateful for Clues.