Cannot define CharField with unlimited length
|Reported by:||millerdev||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.2|
|Cc:||joe@…, mightyiam, unai@…||Triage Stage:||Accepted|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Model validation throws an error on CharField with a null max_length:
class Test(Model): char_field = CharField(max_length=None)
One or more models did not validate:
test.test: "char_field": CharFields require a "max_length" attribute that is a positive integer.
CharField should allow max_length=None, which intuitively means there is no maximum length. This is a perfectly valid use case. Postgres, for example, supports varchar/text columns without a length limit, but Django appears to have no way to define such a column in a model class.
The model validation code looks like this (django/core/management/validation.py:40):
if isinstance(f, models.CharField): try: max_length = int(f.max_length) if max_length <= 0: e.add(opts, '"%s": CharFields require a "max_length" attribute that is a positive integer.' % f.name) except (ValueError, TypeError): e.add(opts, '"%s": CharFields require a "max_length" attribute that is a positive integer.' % f.name)
It should be changed to something this:
if isinstance(f, models.CharField) and f.max_length is not None: ...
The FileField does not happen to throw this error because it is not a derivative of CharField. However, the SQL generated for FileField is not correct when max_length=None, so that would need to be addressed as well.
Change History (17)
comment:1 Changed 5 years ago by akaariai
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Resolution set to invalid
- Status changed from new to closed
comment:3 Changed 4 years ago by joe@…
- Cc joe@… added
- Easy pickings unset
- Resolution invalid deleted
- Severity set to Normal
- Status changed from closed to reopened
- Type set to New feature
- UI/UX unset
comment:4 Changed 4 years ago by aaugustin
- Triage Stage changed from Unreviewed to Design decision needed