如果 decimal_places == max_digits,Django DecimalField 不接受 0

标签 django django-models django-forms

我在模型上定义了一个 DecimalField,其 decimal_places 属性等于 max_digits 属性(用于存储 < 1 的值),如下所示:

class MyModel(Model):
    my_field = DecimalField(max_digits=5, decimal_places=5)

在一个表单中(例如 django 管理站点),这个字段不会取 0,我得到以下错误:

Ensure that there are no more than 0 digits before the decimal point.

但它需要 0.0 :/

有人遇到同样的问题吗?我找不到关于它的帖子。这是错误还是这种行为有原因?

最佳答案

max_digit = 小数点后的位数 + 小数点前的位数。 因此,如果 max_digit == decimal_digit,则小数点前的位数最多为 0。

关于如果 decimal_places == max_digits,Django DecimalField 不接受 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38118250/

相关文章:

python - django - 动态管理器

django - 当表单无效时, View 未返回 HttpResponse 对象错误 : django

html - request.FILES 在文件上传时始终为空

python - 添加/更改表单中的 ForeignKey 字段 - Django admin

django - 如何在自定义用户模型中使用 django-guardian

python - 如何将字典键与模型 ID 匹配

Django 错误 : 'NoneType' object is not subscriptable

python - type str 没有定义 __round__ 方法/如何获取表单字段? django 和cropper.js?

Android-Django用户认证

python - 如何使用特定格式在 Django 模板上获取本地化日期?