Check out validators !
First define your validator:
from django.core.exceptions import ValidationError
def validate_current_century(value):
if value < 2000 or value > 2100:
raise ValidationError(u'%s is not a valid year!' % value)
Now you can use it in your model field:
class Completion(models.Model):
start_date = models.DateField(validators=[validate_current_century])
end_date = models.DateField(validators=[validate_current_century])
And also in the form field:
from django import forms
class MyForm(forms.Form):
current_century_field = forms.DateField(validators=[validate_current_century])
More details in the documents related to the above.
source
share