summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/inspectdb.py3
-rw-r--r--django/db/backends/base/features.py3
-rw-r--r--django/db/backends/postgresql/base.py8
-rw-r--r--django/db/backends/postgresql/features.py1
-rw-r--r--django/db/models/fields/__init__.py22
5 files changed, 32 insertions, 5 deletions
diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py
index 51d7866503..992c523a8e 100644
--- a/django/core/management/commands/inspectdb.py
+++ b/django/core/management/commands/inspectdb.py
@@ -339,7 +339,8 @@ class Command(BaseCommand):
# Add max_length for all CharFields.
if field_type == "CharField" and row.display_size:
- field_params["max_length"] = int(row.display_size)
+ if (size := int(row.display_size)) and size > 0:
+ field_params["max_length"] = size
if field_type in {"CharField", "TextField"} and row.collation:
field_params["db_collation"] = row.collation
diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py
index b5b6c5b55d..ef1fe88336 100644
--- a/django/db/backends/base/features.py
+++ b/django/db/backends/base/features.py
@@ -345,6 +345,9 @@ class BaseDatabaseFeatures:
# Set to (exception, message) if null characters in text are disallowed.
prohibits_null_characters_in_text_exception = None
+ # Does the backend support unlimited character columns?
+ supports_unlimited_charfield = False
+
# Collation names for use by the Django test suite.
test_collations = {
"ci": None, # Case-insensitive.
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
index ceea1bebad..99403f5322 100644
--- a/django/db/backends/postgresql/base.py
+++ b/django/db/backends/postgresql/base.py
@@ -80,6 +80,12 @@ from .operations import DatabaseOperations # NOQA isort:skip
from .schema import DatabaseSchemaEditor # NOQA isort:skip
+def _get_varchar_column(data):
+ if data["max_length"] is None:
+ return "varchar"
+ return "varchar(%(max_length)s)" % data
+
+
class DatabaseWrapper(BaseDatabaseWrapper):
vendor = "postgresql"
display_name = "PostgreSQL"
@@ -92,7 +98,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
"BigAutoField": "bigint",
"BinaryField": "bytea",
"BooleanField": "boolean",
- "CharField": "varchar(%(max_length)s)",
+ "CharField": _get_varchar_column,
"DateField": "date",
"DateTimeField": "timestamp with time zone",
"DecimalField": "numeric(%(max_digits)s, %(decimal_places)s)",
diff --git a/django/db/backends/postgresql/features.py b/django/db/backends/postgresql/features.py
index 49658cd267..6c20dd87f0 100644
--- a/django/db/backends/postgresql/features.py
+++ b/django/db/backends/postgresql/features.py
@@ -110,3 +110,4 @@ class DatabaseFeatures(BaseDatabaseFeatures):
has_bit_xor = property(operator.attrgetter("is_postgresql_14"))
supports_covering_spgist_indexes = property(operator.attrgetter("is_postgresql_14"))
+ supports_unlimited_charfield = True
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index e3b47d173c..060e1be605 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -817,9 +817,14 @@ class Field(RegisterLookupMixin):
# exactly which wacky database column type you want to use.
data = self.db_type_parameters(connection)
try:
- return connection.data_types[self.get_internal_type()] % data
+ column_type = connection.data_types[self.get_internal_type()]
except KeyError:
return None
+ else:
+ # column_type is either a single-parameter function or a string.
+ if callable(column_type):
+ return column_type(data)
+ return column_type % data
def rel_db_type(self, connection):
"""
@@ -1130,14 +1135,19 @@ class BooleanField(Field):
class CharField(Field):
- description = _("String (up to %(max_length)s)")
-
def __init__(self, *args, db_collation=None, **kwargs):
super().__init__(*args, **kwargs)
self.db_collation = db_collation
if self.max_length is not None:
self.validators.append(validators.MaxLengthValidator(self.max_length))
+ @property
+ def description(self):
+ if self.max_length is not None:
+ return _("String (up to %(max_length)s)")
+ else:
+ return _("String (unlimited)")
+
def check(self, **kwargs):
databases = kwargs.get("databases") or []
return [
@@ -1148,6 +1158,12 @@ class CharField(Field):
def _check_max_length_attribute(self, **kwargs):
if self.max_length is None:
+ if (
+ connection.features.supports_unlimited_charfield
+ or "supports_unlimited_charfield"
+ in self.model._meta.required_db_features
+ ):
+ return []
return [
checks.Error(
"CharFields must define a 'max_length' attribute.",