This is a classic problem with django models / forms, etc.! The code datetime.datetime.now().year inside the validator is executed only once, when the source file is read for the first time. It is not executed every time a form is submitted! So it mattered 2016 when it was first executed and still has the same meaning.
To quickly see my point, restart the application server - then it will be fine (the model will be reevaluated and will receive 2017 as datetime.date.today (). Year!). Of course, in order to really fix this problem, you must change the validation logic to use a special validator (MaxValueValidator cannot be used), which will run every time your form is submitted.
For instance:
def my_year_validator(value): if value < 1900 or value > datetime.datetime.now().year: raise ValidationError( _('%(value)s is not a correcrt year!'), params={'value': value}, ) ... # And then in your model: year = models.PositiveSmallIntegerField( null=True, blank=True, validators=[my_year_validator], verbose_name="Year" )
source share