I want to validate whether a user's age (birth_date
) is 18+, this is my current code though it isn't working:
...
def min_date():
return (date.today() - timedelta(days=6570))
class User(BaseModel, RemovabledModel, AbstractUser):
birth_date = models.DateField(
verbose_name=_('birth date'),
help_text=_('Birth date'),
null=True,
blank=True,
validators=[
MinValueValidator(
limit_value=min_date(),
message=("User must be 18 or older")
),
]
)
This runs and is functional, but I can still set a user's age to today's date without the application throwing any error message like it would with other validators.
Edit: In case it is relevant, this is related validator:
def min_date():
return (date.today() - timedelta(days=6570))
class SignUpSerializer(serializers.Serializer):
birth_date = serializers.DateField(
write_only=True,
required=True,
validators=[
MinValueValidator(
limit_value=min_date(),
message=("User must be 18 or older") ),
]
)
You can fix the validation on the model like so (This also means you can omit the custom field from your serializer):
Relevant documentation
Custom User Model
How it's currently set up on the model:
On the date field in the User model; you are calling the min_date() function inside the validator, say you were to migrate.
You head off for the day.
Then the next day; you get back to development; you makemigrations, migrate; and it should say no changes made, but there is a change to the date field.
The date inside the min_date function will be incremented.
The migration doesn't know you are calling a function in this case; it just sees the value.