Opened 10 years ago
Closed 10 years ago
#23138 closed Cleanup/optimization (wontfix)
please add min_length to django.db.models.fields.Field
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Use case is that an application may have requirements for a CharField
to always have a minimum of N characters (e.g. git commit sha1 needs 40characters); but the application may not use forms. Validators for min_length
and max_length
could move from the forms.fields to models.fields.
https://docs.djangoproject.com/en/dev/ref/forms/fields/#charfield
Change History (5)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
If we added min_length
to model fields, I'd think it should be enforced at the database level. Do all of the core databases support minimum length validation? (my guess is no.) In that case, it may be better to solve your use case (Python level validation) by adding a MinLengthValidator()
to your field.
comment:5 by , 10 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
I think it could be implemented in PostgreSQL using a CHECK constraint, but MySQL doesn't enforce them.
edge case:
syncdb
command can inspect the fields and see thatmin_length
==max_length
and mysql backend could useCHAR(N)
instead ofVARCHAR(N)
which results in less bytes being consumed, which could be important savings with lots of records.see https://github.com/django/django/pull/2921.