FixedCharField

Django’s CharField uses the VARCHAR data type, which uses variable storage space depending on string length. This normally saves storage space, but for columns with a fixed length, it adds a small overhead.

The alternative CHAR data type avoids that overhead, but it has the surprising behaviour of removing trailing space characters, and consequently ignoring them in comparisons. FixedCharField provides a Django field for using CHAR. This can help you interface with databases created by other systems, but it’s not recommended for general use, due to the trialing space behaviour.

Docs: MySQL / MariaDB.

class django_mysql.models.FixedCharField(*args, max_length: int, **kwargs)

A subclass of Django’s Charfield that uses the CHAR data type. max_length is as with CharField, but must be within the range 0-255.

For example:

from django_mysql.models import FixedCharField


class Address(Model):
    zip_code = FixedCharField(length=5)