类型对象“购物车”在Django网站中没有属性“对象”

时间:2019-04-03 11:40:36

标签: python django

我正在尝试通过观看一些教程来学习在电子商务网站中制作购物车。我在运行代码时遇到错误,尽管在本教程中可以正常工作,但在我的代码中却给出类似这样的错误。我不知道为什么为什么要尝试几次检查代码,但仍然无法弄清。

  

类型对象“购物车”没有属性“对象”

这些是我的密码

  

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)
  

小推车/视图

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 个答案:

答案 0 :(得分:2)

因此,

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

您已在object模型中将model manager分配为属性Cart。因此,将其重命名为objects