Django DecimalField min value validation error

Question:

I need a decimal field in my form with a minimal value of 0.20, maximal value of 20 with 2 decimal places as you can see in the code bellow.

forms.DecimalField(min_value=0.20, max_value=20.00, max_digits=4, decimal_places=2, initial=1)

The problem occures when you put 0.20 in, because you get a validation error:

Ensure this value is greater than or equal to 0.20.

I’m using python 3.10 and Django 4.0.4

Asked By: Marian

||

Answers:

You can implement your own custom validator also make sure you take min / max values from configurations / settings file instead of hardcoding .

https://www.geeksforgeeks.org/custom-field-validations-in-django-models/

Answered By: Vikram Patil

use Decimal for min_value or max_value:

forms.DecimalField(min_value=Decimal('0.20'), max_value=Decimal('20.00'), max_digits=4, decimal_places=2, initial=1)
Answered By: wing greate