Reputation: 8851
I'm working on a Django app, and I was wondering if there is any reason to explicitly set a max_length
for a model field (TextField specifically, but I'm interested for all fields).
Does it reduce the table size, or improve efficiency? Does a smaller max_length have advantages over a larger one?
Upvotes: 3
Views: 3698
Reputation: 4493
You need to set it in CharField since it defaults to None, I believe. But there are some cases, like phone number, or ssn, or US State abbreviations where it will guide the user to type in a certain correct length of characters. According to the docs: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.TextField
class TextField([**options]) A large text field. The default form widget for this field is a Textarea.
Changed in Django 1.7: If you specify a max_length attribute, it will be reflected in the Textarea widget of the auto-generated form field. However it is not enforced at the model or database level. Use a CharField for that.
Upvotes: 2