Opened 8 months ago

Closed 8 months 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 Changed 8 months ago by dnozay

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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.

see https://github.com/django/django/pull/2921.

comment:2 Changed 8 months ago by timo

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 Changed 8 months ago by dnozay

do all of the core databases support maximum length validation?

comment:4 Changed 8 months ago by charettes

I think only SQLite doesn't enforce them.

comment:5 Changed 8 months ago by timo

  • Resolution set to wontfix
  • Status changed from new to closed

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