113

This is my model:

class Position(models.Model):
    map = models.ForeignKey(Map,primary_key=True)
    #members=models.CharField(max_length=200)
    LatLng = models.CharField(max_length=40000)
    infowindow = models.CharField(max_length=40000)

But it can't run. What is the max size of the max_length parameter?

daaawx
  • 3,273
  • 2
  • 17
  • 16
zjm1126
  • 63,397
  • 81
  • 173
  • 221

5 Answers5

138

That depends on the database backend. The Django DB Docs will tell you, that max_length=255 is guaranteed to work always.

If you need something of the amount you've specified in your question, I'd suggest to use a TextField.

Haes
  • 12,891
  • 11
  • 46
  • 50
  • 2
    With this being the case coupled with the fact that for the DB I use it also limits the unique option to 255, I personally stick to the mantra that if I need a CharField that is bigger than 255 I make it a TextField. – digitaldreamer Apr 08 '10 at 06:35
  • 13
    TextField's max size is also DB dependant. For MySQL, its somewhere around 4 trillion. – Justin Abrahms Apr 08 '10 at 19:45
  • 2
    In MySQL Charfield is a varchar, max 65,535 (but this is also the max row size), see http://stackoverflow.com/questions/13506832/what-is-the-mysql-varchar-max-size. There is no penalty of using large max size, MySQL will use what it actually needs. Note that Textfields are handled differently, with performance implications, so it's better to index with other fields in another table (the more indexed rows per DB page the better), find what you need, then with the now known key get the Textfield. – Aviah Laor Feb 05 '16 at 07:57
  • 2
    While TextField is an ok choice for >255 strings, it does not enforce `max_length` at the model (or database) level. This might complicate some validation logic unlike in the case of CharField that allows you to specify a `max_length` that Django does enforce. – elnygren Mar 02 '17 at 17:44
  • 1
    The linked paragraph says: "Any fields that are stored with VARCHAR column types have their max_length restricted to 255 characters if you are using unique=True for the field. This affects CharField, SlugField." – Chris Aug 10 '17 at 14:58
  • @AviahLaor As explained in [this answer](https://stackoverflow.com/a/4849030), using TextField does not always have a negative impact in performance. At least in Postgres it uses the same underlying type than CharField. – Enric Calabuig Apr 26 '19 at 07:43
  • things will change for mysql if you add an index, max_length will drop to 255 (same as when using unique) – benzkji Jun 22 '20 at 08:24
8

Use TextField instead

infowindow = models.TextField()
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
tshubham7
  • 171
  • 2
  • 4
2

It depends on the backend database. If you use MySQL 5.6 then there is a limit of 65,535. If you are using above that then it will allow for 1024 also but not beyond that.

class Demo(models.Model):
    max_test = models.CharField(max_length=1024)

I ran above on MySQL 5.7

  • 1
    If this will be working depends on the database backend used as already mentioned in the accepted answer. While this could work, a better solution is to use TextField. – colidyre Sep 28 '18 at 07:25
2

It really depends on the database you use for the model. PostgreSQL, MySQL and so on have different settings and limits.

If you really need a long string or not sure how long the variable would be, always safe to just go with variable=models.TextField().

elcortegano
  • 2,444
  • 11
  • 40
  • 58
2

From the django docs

Any fields that are stored with VARCHAR column types may have their max_length restricted to 255 characters if you are using unique=True for the field. This affects CharField, SlugField. See the MySQL documentation for more details.

If a CharField is unique, then its max_length is 255 characters. Otherwise, its max_length on MySQL VARCHAR, 65535 characters.

Paolo
  • 20,112
  • 21
  • 72
  • 113
Sincere_Ye
  • 21
  • 2