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


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.

Change History (5)

comment:1 by Damien Nozay, 10 years ago

edge case: syncdb command can inspect the fields and see that min_length== max_length and mysql backend could use CHAR(N) instead of VARCHAR(N) which results in less bytes being consumed, which could be important savings with lots of records.


comment:2 by Tim Graham, 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:3 by Damien Nozay, 10 years ago

do all of the core databases support maximum length validation?

comment:4 by Simon Charette, 10 years ago

I think only SQLite doesn't enforce them.

comment:5 by Tim Graham, 10 years ago

Resolution: wontfix
Status: newclosed

I think it could be implemented in PostgreSQL using a CHECK constraint, but MySQL doesn't enforce them.

Note: See TracTickets for help on using tickets.
Back to Top