在看<<django by example>>的第八章利用paypal付款那里报错
AttributeError: 'int' object has no attribute 'quantize'
错误代码定位提示为views.py中
'amount': '%.2f' % order.get_total_cost().quantize(Decimal('.01')),
解决方法很简单将语句改为
'amount': '%.2f' % Decimal(order.get_total_cost()).quantize(Decimal('.01'))
但找到个很有意思的事,可以先看相关语句(只放出了函数语句,和Model的具体语句)
class Product(models.Model): price = models.DecimalField(max_digits=10, decimal_places=2)
class Order(models.Model): def get_total_cost(self): return sum(item.get_cost() for item in self.items.all()) class OrderItem(models.Model): price = models.DecimalField(max_digits=10, decimal_places=2) quantity = models.PositiveIntegerField(default=1) def get_cost(se