├── .gitignore ├── README.rst ├── django_postgres_unlimited_varchar.py └── pyproject.toml /.gitignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | *.egg-info 3 | poetry.lock 4 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | A tiny app adding support unlimited ``varchar`` fields (no specified max length) in Django/Postgres. 2 | 3 | Usage:: 4 | 5 | from django.db import models 6 | from django_postgres_unlimited_varchar import UnlimitedCharField 7 | 8 | class Person(models.Model): 9 | name = UnlimitedCharField() 10 | ... 11 | 12 | Why? 13 | 14 | Out of the box, Django has two fields for text: 15 | 16 | * ``CharField``, which is for single-line text, and has a required maximum length (the ``max_length`` argument). In the database, this creates a field of type ``varchar(LENGTH)``. 17 | * ``TextField``, which is for multi-line text, and has no maximum length. In the database, this creates a field of type ``text``. 18 | 19 | Clearly missing is a third type: single-line, no max length. Postgres supports this as the ``varchar`` type (note the lack of a length). 20 | 21 | This field adds that type. AFAIK there isn't any performance hit in using this, so it's suitable for any situation where there isn't a clear required max length. 22 | -------------------------------------------------------------------------------- /django_postgres_unlimited_varchar.py: -------------------------------------------------------------------------------- 1 | __version__ = "1.1.1" 2 | 3 | from django.db import models 4 | 5 | 6 | class UnlimitedCharField(models.CharField): 7 | def __init__(self, *args, **kwargs): 8 | # for Django >= 3.2 9 | self.db_collation = kwargs.pop("db_collation", None) 10 | 11 | # not a typo: we want to skip CharField.__init__ because that adds the max_length validator 12 | super(models.CharField, self).__init__(*args, **kwargs) 13 | 14 | def check(self, **kwargs): 15 | # likewise, want to skip CharField.__check__ 16 | return super(models.CharField, self).check(**kwargs) 17 | 18 | def db_type(self, connection): 19 | return "varchar" 20 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "django-postgres-unlimited-varchar" 3 | version = "1.1.1" 4 | description = "A tiny app adding support unlimited varchar fields in Django/Postgres." 5 | authors = ["Jacob Kaplan-Moss "] 6 | readme = "README.rst" 7 | 8 | [tool.poetry.dependencies] 9 | django = "^2 || ^3 || ^4" 10 | python = "^3.6" 11 | 12 | [build-system] 13 | requires = ["poetry>=0.12"] 14 | build-backend = "poetry.masonry.api" 15 | --------------------------------------------------------------------------------