Resizable Text/Binary Fields¶
Django’s TextField and
BinaryField fields are fixed at the MySQL level to
use the maximum size class for the BLOB and TEXT data types. This is
fine for most applications, however if you are working with a legacy database,
or you want to be stricter about the maximum size of data that can be stored,
you might want one of the other sizes.
The following field classes are simple subclasses that allow you to provide an extra parameter to determine which size class to use. They work with migrations, allowing you to swap them for the existing Django class and then use a migration to change their size class. This might help when taking over a legacy database for example.
- class django_mysql.models.SizedTextField(size_class: int, **kwargs)¶
A subclass of Django’s
TextFieldthat allows you to use the other sizes ofTEXTdata type. Setsize_classto:1for aTINYTEXTfield, which has a maximum length of 255 bytes2for aTEXTfield, which has a maximum length of 65,535 bytes3for aMEDIUMTEXTfield, which has a maximum length of 16,777,215 bytes (16MiB)4for aLONGTEXTfield, which has a maximum length of 4,294,967,295 bytes (4GiB)
- class django_mysql.models.SizedBinaryField(size_class, **kwargs)¶
A subclass of Django’s
BinaryFieldthat allows you to use the other sizes ofBLOBdata type. Setsize_classto:1for aTINYBLOBfield, which has a maximum length of 255 bytes2for aBLOBfield, which has a maximum length of 65,535 bytes3for aMEDIUMBLOBfield, which has a maximum length of 16,777,215 bytes (16MiB)4for aLONGBLOBfield, which has a maximum length of 4,294,967,295 bytes (4GiB)