django 代码${order.get_cart_total}}中的整数对象不可调用

k10s72fa  于 2023-01-27  发布在  Go
关注(0)|答案(4)|浏览(70)

我在模型中有下面的代码,当调用get_cart_total int时,+的操作数类型不可调用或不受支持:“int”和“method "外观
我希望从get_cart_total获得总计

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, blank=True, null=True)
    date_ordered = models.DateTimeField(auto_now_add=True)
    complete = models.BooleanField(default=False, null=True, blank=False)
    transaction_id = models.CharField(max_length=200, null=True)

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

    @property
    def get_cart_items(self):
        orderitems = self.orderitem_set.all()
        total = sum([item.quantity for item in orderitems])
        return total

    @property
    def get_cart_total(self):
    
        items = self.get_cart_items()
        for item in items:
            total += item.get_total()
        return total

    

class OrderItem(models.Model):
    ORDER_ITEM_TYPE = (
        ('type1', 'Collection1'),
        ('type2', 'Collection2'),
    )
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    collection_type = models.CharField(max_length=255, choices=ORDER_ITEM_TYPE)
    collection1 = models.ForeignKey(Collection1, on_delete=models.SET_NULL, null=True, blank=True)
    collection2 = models.ForeignKey(Collection2, on_delete=models.SET_NULL, null=True, blank=True)
    quantity = models.IntegerField()

    def get_total(self):
        if self.collection_type == "type1":
            return self.collection1.price * self.quantity
        elif self.collection_type == "type2":
            return self.collection2.price * self.quantity
ljo96ir5

ljo96ir51#

因为self.get_cart_items是一个属性,所以你不需要调用它,你可以删除@property装饰器,或者只期望self.get_cart_items已经是一个整数,而不是一个方法。

@property
    def get_cart_total(self):
    
-       items = self.get_cart_items()
+       items = self.get_cart_items
        for item in items:
            total += item.get_total()
        return total
ycl3bljg

ycl3bljg2#

您已经将get_cart_items设置为property,它被视为对象的属性,而不是方法。您需要从计算中删除该装饰器或()
备选办法1:

@property
def get_cart_items(self):
    orderitems = self.orderitem_set.all()
    total = sum([item.quantity for item in orderitems])
    return total

@property
def get_cart_total(self):
    items = self.get_cart_items
    for item in items:
        total += item.get_total()
    return total

备选方案二:

def get_cart_items(self):
    orderitems = self.orderitem_set.all()
    total = sum([item.quantity for item in orderitems])
    return total

@property
def get_cart_total(self):
    items = self.get_cart_items()
    for item in items:
        total += item.get_total()
    return total
j8ag8udp

j8ag8udp3#

  • 您的get_cart_items是一个属性方法,它是不可调用的,因此当您要使用此方法时,请删除()
  • get_cart_items()返回一个int值,它是项目数量的总和,因此在代码items = self.get_cart_items()中不能循环整数值

相应地修复代码

@property
    def get_cart_items(self):
        return self.orderitem_set.all()

    @property
    def get_cart_total(self):
        items = self.get_cart_items
        for item in items:
            total += item.get_total()
        return total
xqk2d5yq

xqk2d5yq4#

几点意见:
1.'get_cart_items'是一个属性(@property decorator),因此您应该将其视为属性。更改:items = self.get_cart_items()items = self.get_cart_items,或者删除@属性装饰器。
1.'get_cart_items'未返回OrderItem对象列表,而是返回项目数量总和(整数)
1.“get_cart_total”方法中未初始化“total”变量。
对于'get_cart_total'方法,请尝试类似下面的操作:

def get_cart_total(self):
    total = 0
    for item in self.orderitem_set.all():
        total += item.get_total()
    return total

相关问题