DJANGO DECIMALFIELD最小值验证错误

发布于 2025-02-10 23:46:40 字数 319 浏览 1 评论 0 原文

我的形式需要一个十进制字段,最小值为0.20,最大值为20,带有2个小数位置,如您在Code Bellow中所见。

forms.DecimalField(min_value=0.20, max_value=20.00, max_digits=4, decimal_places=2, initial=1)

当您将0.20输入时,问题会发生,因为您会遇到验证错误:
确保此值大于或等于0.20。

我正在使用 python 3.10 django 4.0.4

I need a decimal field in my form with a minimal value of 0.20, maximal value of 20 with 2 decimal places as you can see in the code bellow.

forms.DecimalField(min_value=0.20, max_value=20.00, max_digits=4, decimal_places=2, initial=1)

The problem occures when you put 0.20 in, because you get a validation error:

Ensure this value is greater than or equal to 0.20.

I'm using python 3.10 and Django 4.0.4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂归处 2025-02-17 23:46:40

使用小数为min_value或max_value:

forms.DecimalField(min_value=Decimal('0.20'), max_value=Decimal('20.00'), max_digits=4, decimal_places=2, initial=1)

use Decimal for min_value or max_value:

forms.DecimalField(min_value=Decimal('0.20'), max_value=Decimal('20.00'), max_digits=4, decimal_places=2, initial=1)
谈下烟灰 2025-02-17 23:46:40

您可以实现自己的自定义验证器,还要确保从配置 /设置文件中获取最小 /最大值,而不是硬编码。

You can implement your own custom validator also make sure you take min / max values from configurations / settings file instead of hardcoding .

https://www.geeksforgeeks.org/custom-field-validations-in-django-models/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文