Charfield max_length Requirements

We are three students working on a Full-Stack semester project using Django with SQLite as the backend database.

In our models, we used multiple instances of CharField without specifying the max_length parameter. Since we are using SQLite, and based on our understanding of the Django documentation, the max_length attribute is not strictly enforced in development environments using SQLite, so we did not include it.

We tested our project successfully on different platforms:

  • Windows.
  • Linux Mint
  • macOS
  • Rapberry Pi OS (Debian-bassed)

In all these environments, the project ran without any errors.
However, when our professor cloned and tested the project on his device, the following error occurred:

TypeError: CharField.__init__() missing 1 required positional argument: 'max_length'

Here is a relevant section of our environment setup:
requirements.txt file:

asgiref==3.8.1
certifi==2025.6.15
charset-normalizer==3.4.2
Django==5.2.3
django-actual-admin-docs==0.5
django-extensions==4.1
django-filter==25.1
django-stubs==5.2.0
django-stubs-ext==5.2.0
djangorestframework==3.16.0
djangorestframework-stubs==3.16.0
docutils==0.21.2
idna==3.10
linkify-it-py==2.0.3
Markdown==3.8
markdown-it-py==3.0.0
mdit-py-plugins==0.4.2
mdurl==0.1.2
pillow==11.2.1
requests==2.32.4
sqlparse==0.5.3
types-PyYAML==6.0.12.20250516
types-requests==2.32.4.20250611
typing_extensions==4.14.0
tzdata==2025.2
uc-micro-py==1.0.3
urllib3==2.4.0

And in settings.py:

DATABASES: Dict[str, Dict[str, Any]] = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

One example Charfield usage:

class Category(models.Model):
    category = models.CharField(null=False, unique=True) # type: ignore

We are aware that Django documentation states max_length is required for CharField. However, it seems that the enforcement of this requirement varies depending on Django version, database engine, or possibly linting tools.

Since the project runs fine in our environments and fails only on our professor’s machine, we would like to know:

  • Why is this error raised only in some environments?
  • Is it possible that the Django version or a stricter database configuration enforces this requirement more strictly?
  • What is the best practice for handling CharField when using SQLite during development?

Thank you for your help and clarification.

Welcome @mostaffa !

The ability to not provide the max_length parameter for SQLite is a change in Django 5.2.

In the Models portion of the Minor features section at: Django 5.2 release notes | Django documentation | Django :

  • CharField.max_length is no longer required to be set on SQLite, which supports unlimited VARCHAR columns.

It’s also highlighted in the current version of the docs for CharField

This change was introduced for PostgreSQL in Django 4.2. (Django 4.2 release notes | Django documentation | Django)

2 Likes

Thank you @KenWhitesell and Happy birthday :bouquet:

so could this error appear in other environments even if it uses SQLite backend database?

(Thank you)

Yes. You will get this same error in any environment using a version of Django earlier than 5.2.

1 Like