Django 管理表单中的小数字段
我有一个包含一个字段的模型:
TAX_CHOICES = (
(10.5, '10.5%'),
(17.5, '17.5%'),
(30, '30%'),
(33, '33%'),
)
taxBracket = models.DecimalField(max_digits=4, decimal_places=2, choices=TAX_CHOICES, default=10.5)
我有现有的 Django 表单,可以在我的应用程序中使用,以允许用户成功输入他们的税级。但是,我需要通过 Django 管理界面手动添加一些用户信息,但当我选择 10.5 或 17.5 值时,出现如下错误:
Value Decimal('10.5') is not a valid choice
我从阅读 Django 文档中了解到,管理表单将 DecimalField 类型视为“文本”输入,但不确定我是否应该查看模型定义或管理表单的自定义。我目前没有在此项目中定义用于管理站点自定义的 admin.py。
I have a model which includes a field:
TAX_CHOICES = (
(10.5, '10.5%'),
(17.5, '17.5%'),
(30, '30%'),
(33, '33%'),
)
taxBracket = models.DecimalField(max_digits=4, decimal_places=2, choices=TAX_CHOICES, default=10.5)
I have existing Django forms which work in my application to allow users to enter their tax bracket successfully. However, I need to manually add some user information via the Django Admin interface but am getting an error as follows when I select 10.5 or 17.5 values:
Value Decimal('10.5') is not a valid choice
I understand from reading the Django docs that the Admin form treats DecimalField types as a 'text' input, but am not sure whether I should be looking at the Model definition or a customisation of the Admin form. I do not currently have an admin.py defined in this project for Admin site customisation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将字段选项定义为浮点数。浮点数
10.5
不等于小数Decimal("10.5")
。将选项定义为字符串或小数:
You have defined the field options as floats. The float
10.5
does not equal the decimalDecimal("10.5")
.Either define the choices as strings or as decimals: