"从列表或元组创建Decimal时无效的元组大小"在DecimalField更新

时间:2016-12-23 15:18:01

标签: python django django-models

我有以下型号:

class Invoice(models.Model):
    debt = models.DecimalField(max_digits = 11, decimal_places = 2)

当我尝试更新现有的发票时:

invoice = get_object_or_404(Invoice, pk=invoice_id)
invoice.debt = '0'
invoice.save()

我收到以下错误:

'Invalid tuple size in creation of Decimal from list or tuple. The list or tuple should have exactly three elements.'

我试过

invoice.debt = 0,
invoice.debt = Decimal('0'),
invoice.debt = 0.0,
invoice.debt = Decimal('0.0')

上始终存在相同的错误
invoice.save()

有什么想法吗? Django 1.10.4,Python 3.4.3,数据库正在使用MySQL 5.5.53-0ubuntu0.14.04.1

1 个答案:

答案 0 :(得分:2)

你在指定的值之后放了一个逗号,所以Python把它变成了一个元素的元组!

invoice.debt = 0,  # == (0,)
invoice.debt = Decimal('0'),  #  == (Decimal('0'),)
invoice.debt = 0.0,  # == (0.0,)
invoice.debt = Decimal('0.0')