Setting minimum length on a model field

Hello if I have a CharField on my model, instead of adding a database constraint to add a minimum length to the field, could I add a validator (MinLengthValidator) to the field to ensure that the field has to be a certain amount of characters? Would this work? Thanks.

That would work for that form - but unless you create a custom admin form it won’t prevent someone from adding a short-field through the admin, and neither would prevent someone from entering a short-field through a different application or directly to the database.

Whether or not that’s an issue is something you would need to determine.

Ken

1 Like

@KenWhitesell Ah okay, so adding a validator to a model field for setting a min_length, would not work at database level?

So how would I accomplish database level validation on a model field (in this case setting a min_length). By using database constraints? Do I even really need to do it if I add validators to the form fields, e.g MinLengthValidator? Or is there any other method I can use if it is recommended to do so?