[Fixed]-Django model field validation without a custom form

21👍

You can look into Django Validators.

https://docs.djangoproject.com/en/dev/ref/validators/

You would put the validator before the class, then set the validator in the Field.

def validate_monday(date):
    if date.weekday() != 0:
        raise ValidationError("Please select a Monday.")

class CalWeek(models.Model):
    start_date = models.DateField(validators=[validate_monday])

Leave a comment