如何限制Django模型中PositiveIntegerfield的最大值?
发布时间:2020-12-20 11:39:55 所属栏目:Python 来源:网络整理
导读:我在模型中有一个字段存储百分比值.如何以比模型验证器do1更严格的方式将可能值限制为0-100范围? 笔记 Model validators will not be run automatically when you save a model,but if you’re using ModelForms 解决方法 你应该使用验证器. from django.db
我在模型中有一个字段存储百分比值.如何以比模型验证器do1更严格的方式将可能值限制为0-100范围?
笔记 > Model validators will not be run automatically when you save a model,but if you’re using ModelForms 解决方法
你应该使用验证器.
from django.db import models from django.core.validators import MaxValueValidator class MyModel(models.Model): percent_field = models.PositiveIntegerField(min_value=0,validators=[MaxValueValidator(100),]) 就个人而言,我宁愿使用Float存储百分比,并使用0-1范围.验证器应该以类似的方式工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |