0

所以这发生在我身上:

thing = ModelClass()
thing.foo = bar()
thing.do_Stuff()
thing.save()  #works fine
thing.decimal_field = decimal_value
thing.save()  #error here

追溯如下:

TypeError at /journey/collaborators/2/

unsupported operand type(s) for ** or pow(): 'Decimal' and 'str'

274.             oH.save()
File "/usr/lib/python2.7/dist-packages/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.7/dist-packages/django/db/models/base.py" in save_base
  543.                         for f in meta.local_fields if not isinstance(f, AutoField)]
File "/usr/lib/python2.7/dist-packages/django/db/models/fields/subclassing.py" in inner
  28.             return func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/db/models/fields/__init__.py" in get_db_prep_save
  787.                 self.max_digits, self.decimal_places)
File "/usr/lib/python2.7/dist-packages/django/db/backends/__init__.py" in value_to_db_decimal
  705.         return util.format_number(value, max_digits, decimal_places)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py" in format_number
  145.         return u'%s' % str(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))

我尝试设置decimal_value为一个decimal.Decimal实例、一个浮点数、一个 int 和一个字符串。除非我将该字段留空,否则我似乎无法保存我的模型实例。

任何想法如何解决这一问题?

4

2 回答 2

1

解决它。我变了:

 decimal_field = models.DecimalField(decimal_places='2',max_digits='6',null=True)

 decimal_field = models.DecimalField(decimal_places=2,max_digits=6,null=True)
于 2013-04-17T21:28:24.310 回答
1

从堆栈跟踪,它看起来像decimal_places这里

return u'%s' % str(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))

是 astr并且您正在尝试执行pow不允许Decimalstr类型的操作 ( )

因此,您可以尝试更改decimal_placesint(decimal_places)或弄清楚为什么它是 astr而不是int.

于 2013-04-17T20:10:13.813 回答