Search code examples
sqldjangodatabasestorage

What's the difference between CharField and TextField in Django?


The documentation says that CharField() should be used for smaller strings and TextField() should be used for larger strings.

Okay, but where is the line drawn between "small" and "large"? What's going on under the hood here that makes this the case?


Solution

  • It's a difference between RDBMS's varchar (or similar) — those are usually specified with a maximum length, and might be more efficient in terms of performance or storage — and text (or similar) types — those are usually limited only by hardcoded implementation limits (not a DB schema).

    PostgreSQL 9, specifically, states that "There is no performance difference among these three types", but AFAIK there are some differences in e.g. MySQL, so this is something to keep in mind.

    A good rule of thumb is that you use CharField when you need to limit the maximum length, TextField otherwise.

    This is not really Django-specific, also.