TextField or Charfield?

In the Django Docs for the TextField they say:

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.

Does this mean that I may define a max_length, but if my area on the frontend is to small than the text might be cropped?
Considering the Charfield which is described as…

A string field, for small- to large-sized strings.

I am wondering "what is a large-sized string - 500, 1000, 10000 characters?

They’re not talking about the physical size of the text area.

If you define max_length, that is the number of characters that the field in the browser will be initialized to accept. (Subject to being changed by JavaScript, malicious users, etc.) The point they’re making is that there’s no validation on the server side.
If you create a form with a TextField max_length of 50, and someone alters that field in their browser to allow for 500 characters - and sends 500 characters, your application is going to need to deal with a 500-character submission. (Or 500K, or 500M if the server will allow a POST that large.)

To some degree it depends upon the database and how Django builds the SQL to construct tables in that database.

For example, MariaDB defines a varchar as being up to 64K (65535) - VARCHAR — MariaDB Documentation

On the other hand, PostgreSQL says that there’s no performace difference between varchar(n) and text, so for that it wouldn’t matter. (See the Note section at PostgreSQL: Documentation: 14: 8.3. Character Types)

1 Like