Vivek S
Vivek S

Reputation: 5550

how to create medium text filed in mysql using django models?

I have created a django model and used TextField for one of the attributes of the model. but when i look into the MYSQL table structure it is created as long text. How can i speify django to create a medium text filed. my model structure:

class ModelExample(models.Model):
    AdditionalReqFields=models.TextField(null=True,blank=True)

Upvotes: 4

Views: 5361

Answers (2)

kjpc-tech
kjpc-tech

Reputation: 570

The django-mysql package includes a SizedTextField that you can set to use MEDIUMTEXT.

https://django-mysql.readthedocs.io/en/latest/model_fields/resizable_text_binary_fields.html?highlight=mediumtext#django_mysql.models.SizedTextField

For example:

from django_mysql.models.fields import SizedTextField

AdditionalReqFields = SizedTextField(size_class=3, null=True, blank=True)

Upvotes: 0

David Wolever
David Wolever

Reputation: 154594

In short, you can't.

In longer, if a CharField (backed by VARCHAR) is suitable, that's an option, or create your own subclass of TextField and override the db_type(self, connection) method.

See also: https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.models.Field.db_type

Upvotes: 5

Related Questions