只允许正十进制数 [英] Allow only positive decimal numbers
本文介绍了只允许正十进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Django模型中,我创建了一个十进制字段,如下所示:
Within my Django models I have created a decimal field like this:
price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12)
显然,价格上涨没有意义为负数或零。有没有办法将十进制数限制为仅正数?
Obviously it makes no sense for the price to be negative or zero. Is there a way to limit the decimal number to only positive numbers?
还是我必须使用表单验证来捕获它?
Or do I have to capture this using form validation?
推荐答案
使用 MinValueValidator
。
price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12, validators=[MinValueValidator(Decimal('0.01'))])
这篇关于只允许正十进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文