Cannot define CharField with unlimited length
|Reported by:||millerdev||Owned by:||Aron Podrigal|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||joe@…, mightyiam, unai@…, Aron Podrigal||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
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 (23)
comment:3 Changed 5 years ago by
|Status:||closed → reopened|
|Type:||→ New feature|
comment:18 Changed 15 months ago by
|Cc:||Aron Podrigal added|
|Owner:||changed from nobody to Aron Podrigal|
|Status:||new → assigned|
|Version:||1.2 → master|