Django IntegerRangeField验证失败 [英] Django IntegerRangeField Validation failing

查看:74
本文介绍了Django IntegerRangeField验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现 IntegerRangeField()表示年龄范围字段。不幸的是文档没有说明如何验证上限和下限。

I'm trying to implement the IntegerRangeField() for an age range field. Unfortunately the documentation doesn't say how to validate the upper and lower bounds.

我从模型中进行了如下尝试:

I tried it from the model like so:

class SomeModel(models.Model):
    age_range = IntegerRangeField(default='(0,100)', blank=True, validators=[MinValueValidator(1), MaxValueValidator(100)])

问题是,无论您在字段中输入什么内容,Django都会抛出ValidationError:

The problem is, no matter what you put in the field, Django throws a ValidationError:


该值必须小于或等于100

The value must be less than or equal to 100

如果我在该字段中未放置任何内容,它不会放入默认范围,并且会失败,并抱怨IntegrityError。

Also if I put nothing in the field, it doesn't put the default range, and fails, complaining about an IntegrityError.

所以,我尝试从表单对象执行此操作:

So, I tried doing this from the form object:

class SomeForm(forms.ModelForm):
    age_range = IntegerRangeField(validators=[MinValueValidator(1), MaxValueValidator(100)])

但这根本不起作用。我在字段中输入的任何数字都会保存。我在做什么错了?

But that does nothing at all. Any figure I put in the fields saves. What am I doing wrong?

推荐答案

MinValueValidator MaxValueValidator 用于整数,因此它们是在此处使用的错误验证器。而是将验证器专门用于范围: RangeMinValueValidator RangeMaxValueValidator

The MinValueValidator and MaxValueValidator are for integers, so they are the incorrect validators to use here. Instead use the validators specifically for ranges: RangeMinValueValidator and RangeMaxValueValidator.

这两个验证器都位于模块 django.contrib.postgres.validators 中。

Both of those validators live in the module django.contrib.postgres.validators.

此处是验证器源代码的链接

此外, IntegerRangeField 在Python中表示为 psycopg2.extras.NumericRange 对象,因此在模型中指定 default 参数时,请尝试使用该对象代替字符串。

Also, an IntegerRangeField is represented in Python as a psycopg2.extras.NumericRange object, so try using that instead of a string when you specify your default parameter in the model.

注意:默认情况下, NumericRange 对象包含下限,不包含上限,因此NumericRange(0,100)将包括0,不包括100.您可能需要NumericRange(1,101)。您还可以在 NumericRange 对象中指定一个 bounds 参数,以更改包含/排除的默认值,以代替更改数字值。请参见 NumericRange对象文档

Note: The NumericRange object by default is inclusive of the lower bound and exclusive of the upper bound, so NumericRange(0, 100) would include 0 and not include 100. You probably want NumericRange(1, 101). You can also specify a bounds parameter in your NumericRange object to change the defaults for inclusion/exclusion, in lieu of changing the number values. See the NumericRange object documentation.

例如:

# models.py file
from django.contrib.postgres.validators import RangeMinValueValidator, RangeMaxValueValidator
from psycopg2.extras import NumericRange

class SomeModel(models.Model):
    age_range = IntegerRangeField(
        default=NumericRange(1, 101),
        blank=True,
        validators=[
            RangeMinValueValidator(1), 
            RangeMaxValueValidator(100)
        ]
    )

这篇关于Django IntegerRangeField验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆