0
votes

I am trying to learn to make cart in e-commerce website by watching some tutorial. I get an error while running my code although it works fine in the tutorial but in my code, it gives error something like this. I have no idea why I tried to check my code several time but still I am unable to figure out.

type object 'Cart' has no attribute 'objects'

These are my codes

carts/models.py

from django.conf import settings

from django.db import models
from products.models import Product
User = settings.AUTH_USER_MODEL

# Create your models here.

class CartManager(models.Manager):
    def new(self, user=None):
        user_obj = None
        if user is not None:
            if user.is_authenticated():
                user_obj = user_obj
        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)
    updated     = models.DateTimeField(auto_now=True)
    timestamp   = models.DateTimeField(auto_now_add=True)

    object = CartManager()
    def __str__(self):
        return str(self.id)

carts/view

from django.shortcuts import render

from .models import Cart

# Create your views here.


def cart_home(request):
    cart_id = request.session.get('cart_id', None)
    qs = Cart.objects.filter(id = cart_id)
    if qs.count() == 1:
        cart_obj = qs.first()
        print('Cart id exists')
    else:
        cart_obj = Cart.objects.new(user=request.user)
        request.session['cart_id'] = cart_obj.id
    return render(request, "carts/home.html", {})
1

1 Answers

2
votes

Because of this:

class Cart(models.Model):
    ...
    object = CartManager()

You have assigned the model manager as attribute object in Cart model. So rename it to objects.