【发布时间】:2012-09-24 06:55:56
【问题描述】:
我试图强制 Django 始终存储小数点后 2 位的数字,但如果数字是整数,那么它不会存储 .00
def clean_myfield(self):
data = self.cleaned_data['myfield']
self.cleandecimal(data)
return data
def cleandecimal(self, data):
data = Decimal(data).quantize(Decimal('.01'), rounding=ROUND_DOWN)
print 'data ', data
return data
我的打印始终按预期显示,例如4.00 但存储为 4
如何重写 save 方法来存储值而不四舍五入?
【问题讨论】:
-
模型中的字段是什么类型的?
-
抱歉,它们都是标准的
models.DecimalField或派生自models.DecimalField的自定义类型 -
为什么将其存储为
4会出现问题?只要它不将4.10存储为4,我就不会在这里看到问题。
标签: django model save rounding