【问题标题】:Calculate order total in django ecommerce计算 django 电子商务中的订单总额
【发布时间】:2019-07-29 18:53:25
【问题描述】:

所以我正在建立一个电子商务网站来提高我在 python 方面的知识。 到目前为止,我的学习曲线进展顺利。我想知道如何计算购物车总数。

我有我的产品型号

def Products(models.Model):
    price=models.Decimalfield(default=2.50,null=True,decimal_places=2)
    quantity=models.InterField(default=4,null=True)


def Cart(models.Model):
     total=models.DecimalField(default=0.0,decimal_places=2)
     products=models.ManyToManyField(Products,null=True)

假设用户在购物车中添加了 2 件产品

第一个产品

Product.quantity=3, Product.price=$40

第二个产品

Product.quantity=5, Product.price=$20

计算我的购物车总数的最佳方法是什么。

我试过这个,我在购物车模型下创建了一个保存功能

def save(self,*args,**kwargs):
    if self.id:
       total=0.00
       products=self.products.all()
       total=math.fsum(pro.price * pro.quantity for pro in products)
       self.total=total
     super(Cart,self).save(*args,**kwargs)

但这对我不起作用,我需要帮助。提前致谢

【问题讨论】:

标签: python django


【解决方案1】:

代码运行良好,我的字段有错字。感谢大家的贡献。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多