I have an API developed with Django and Django Rest Framework. I've got a model with a DecimalField which should store values of at least one. Therefore, I've defined the field in the following way:
goal = models.DecimalField(decimal_places=2, max_digits=16, validators=[MinValueValidator(1)])
When I create objects calling the API (using DRF's ModelViewSet), I can create objects with negative "goal", so the validator doesn't seem to work (I've got it included in the model serializer).
Any idea?
The validators only run automatically when Django’s
ModelForm
is used. Otherwise, you need to trigger validation yourself, usually by calling the model object’sfull_clean()
method.See https://docs.djangoproject.com/en/1.9/ref/validators/#how-validators-are-run
This is a known bug with Django REST framework 3.3.x. You can temporarily "fix" the issue by downgrading to 3.2.5, or by waiting for 3.3.3 to be released (which should include the fix).
DRF is currently filtering out that validator in Django < 1.9, which is why it is not actually being run.