我不应该提出验证错误。这是示例:
from django.db.models import DecimalField
f = DecimalField(max_digits=9, decimal_places=3)
# got validation error here
# `Ensure that there are no more than 3 decimal places`
f.clean(value=12.123, model_instance=None)
# returns Decimal('12.1230000')
f.to_python(12.123)
# this is absolutely fine
f.clean(value=123456.123, model_instance=None)
# returns Decimal('123456.123')
f.to_python(123456.123)
很显然,Django DecimalField
使用了错误的to_python
实现,该实现最后返回了过多的尾随零,然后验证失败。
该怎么办?
答案 0 :(得分:1)
您必须将值作为字符串而不是浮点数传递。检查一下
from django.db.models import DecimalField
f = DecimalField(max_digits=9, decimal_places=3)
f.clean(value="12.123", model_instance=None)
f.to_python("12.123")
f.clean(value="123456.123", model_instance=None)
f.to_python("123456.123")