Reputation: 8023
I have a TextField
I would like to add a min_length
to.
According to the Django docs there are built in validators you can use to enforce this (https://docs.djangoproject.com/en/1.11/ref/validators/#minlengthvalidator).
I tried to add this to my model with this code:
class MyPage(Page):
...
introduction = models.TextField(
null=True,
blank=False,
validators=[MinLengthValidator(240, message="Must be at least 240 chars.")]
When publishing this page with a value lower than 240, however, I did not get the expected validation error, but instead hit a TypeError
: unorderable types: str() < int()
.
Is there a way to have a min_length
on a TextField
?
Upvotes: 0
Views: 1701
Reputation: 4194
If default MinLengthValidator
isn't work for you, I suggest you to crate a custom validator like below:
from django.core.validators import ValidationError
def max_length_validator(value):
if len(value) < 240:
raise ValidationError("Must be at least 240 chars.")
class MyPage(Page):
introduction = models.TextField(
blank=False, validators=[max_length_validator])
Upvotes: 1