I am able to set the minimum value validator on the model:
class MyModel(Model):
my_field = models.fields.IntegerField(default=250, validators=[MinValueValidator(30)])
Then I create a form using the previous model:
class MyForm(ModelForm):
class Meta:
model = MyModel
fields = ('my_field',)
The form gets validated, the correct error message is displayed (when entered value is <30), but even when entered wrong value, it gets saved on the instance!
I was able to make it work by specifying (again) the min value on the form:
class MyForm(ModelForm):
my_field = form.IntegerField(min_value=30)
class Meta:
model = MyModel
fields = ('my_field',)
But this way the code is not dry - I need to specify the value and field type twice.
Is there any way I could avoid this? Or at least get the min_value from the model validator?