【发布时间】:2016-03-05 09:38:34
【问题描述】:
我知道这个问题已经被问过很多次了,但不知何故没有解决。我在模型 Song 中有这个模型字段。
sent_score = models.DecimalField(default= 0.0,max_digits=4,decimal_places=2,blank=True)
在我看来,我通过以下方式更新发送的分数。
song_item = Song.objects.get(id=id)
com_list = Comment.objects.filter(song_id=id)
com_count = com_list.count()
pos_comments = com_list.filter(sentiment='positive')
pos_count = pos_comments.count()
getcontext().prec = 2
sent_score = Decimal(pos_count)/Decimal(com_count)
但是它仍然给我这个错误:
InvalidOperation: quantize result has too many digits for current context
我也试过这种方式的quantize方法:
sc = Decimal(pos_count)/Decimal(com_count)
Decimal(sc).quantize(Decimal('12.12'), rounding = ROUND_DOWN)
song_item.sent_score = sc
song_item.save()
但它仍然给出同样的错误请告诉我我哪里出错了
【问题讨论】:
标签: django python-2.7 django-models decimal