MaxValueValidator to validate a model

249 views
Skip to first unread message

refreegrata

unread,
Oct 15, 2010, 5:13:10 PM10/15/10
to Django users
Hello list. I want to validate the data in a field of a model.

with this
validators=[MaxValueValidator(14)]
the field is correctly validated

but with this
validators=[MaxValueValidator(14.5)]
always is invalid

Somebody know how use the "MaxValueValidator" when the "max_value" is
a decimal?

thanks for read.

refreegrata

unread,
Oct 15, 2010, 5:18:47 PM10/15/10
to Django users
Now works.
validators=[MaxValueValidator(Decimal('14.5'))

But I must to do an explicit declaration Decimal('14.5').

Somebody knows why?

Steve Holden

unread,
Oct 18, 2010, 8:34:25 AM10/18/10
to django...@googlegroups.com
On 10/15/2010 9:18 PM, refreegrata wrote:
> Now works.
> validators=[MaxValueValidator(Decimal('14.5'))
>
> But I must to do an explicit declaration Decimal('14.5').
>
> Somebody knows why?

Because only decimal numbers can give exact comparisons to arbitrary
decimal values. You would not want your input to validate correctly with
an entry of (say) 14.500000000000001, which might be possible if the
limit was expressed as a floating-point number.

regards
Steve
--
DjangoCon US 2010 September 7-9 http://djangocon.us/

Reply all
Reply to author
Forward
0 new messages