Search code examples
pythondjangoiterableiterable-unpacking

TypeError at /cart/ cannot unpack non-iterable Cart object


I am developing an eCommerce app in Django. I am still developing locally.

As I try to access to http://127.0.0.1:8000/cart/ I get the error:

TypeError at /cart/

cannot unpack non-iterable Cart object

in cart_home
cart_obj, new_obj = Cart.objects.new_or_get(request)
TypeError: cannot unpack non-iterable Cart object

variable  value
request   <WSGIRequest: GET '/cart/'>

and I can't understand what is wrong.

Here is my models.py:

from django.db import models
from django.conf import settings
from products.models import Product

User = settings.AUTH_USER_MODEL

class CartManager(models.Manager):

    def new_or_get(self, request):
        cart_id = request.session.get("cart_id", None)
        print(cart_id)
        qs = self.get_queryset().filter(id=cart_id)
        if qs.count() == 1 :
            new_obj = False
            print('Cart ID exists')
            cart_obj = qs.first()
    
            if request.user.is_authenticated and cart_obj.user is None:
                cart_obj.user = request.user
                cart_obj.save()
        else:   
            print('Cart ID does not exist')     
            cart_obj = Cart.objects.new(user=request.user)
            new_obj = True
            request.session['cart_id'] = cart_obj.id
    
        return cart_obj

    def new(self, user=None):
        user_obj = None
        if user is not None:
            if user.is_authenticated:
                user_obj = user

        return self.model.objects.create(user=user_obj)

class Cart(models.Model):
    user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
    products = models.ManyToManyField(Product, blank=True)
    total = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
    timestamp = models.DateTimeField(auto_now=True)
    updated = models.DateTimeField(auto_now_add=True)

    objects = CartManager()

    def __str__(self):
        return str(self.id)

Here is my views.py:

from django.shortcuts import render
from .models import Cart

def cart_home(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    products = cart_obj.products.all()

    total = 0
    for x in products:
        total += x.price

    print("total")
    print(total)
    cart_obj.total = total
    cart_obj.save()

    return render(request, "carts/home.html", {})

Solution

  • Turns out that I forgot to add an argument (new_obj) in the return statement of models.py > CartManager > new_or_get

    so it returned just one argument, so that my command at views.py > cart_home was trying to assing just one value (cart_obj) to two variables (cart_obj, new_obj).

    So the correction on models.py > CartManager > new_or_get is the following

    def new_or_get(self, request):
        cart_id = request.session.get("cart_id", None)
        print(cart_id)
        qs = self.get_queryset().filter(id=cart_id)
        if qs.count() == 1 :
            new_obj = False
            print('Cart ID exists')
            cart_obj = qs.first()
    
            if request.user.is_authenticated and cart_obj.user is None:
                cart_obj.user = request.user
                cart_obj.save()
        else:   
            print('Cart ID does not exist')     
            cart_obj = Cart.objects.new(user=request.user)
            new_obj = True
            request.session['cart_id'] = cart_obj.id
    
        return cart_obj, new_obj