What is the max size of 'max_length' in Django?

DjangoDjango Models

Django Problem Overview


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?

Django Solutions


Solution 1 - Django

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.

Solution 2 - Django

Use TextField instead

infowindow = models.TextField()

Solution 3 - Django

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

Solution 4 - Django

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().

Solution 5 - Django

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionzjm1126View Question on Stackoverflow
Solution 1 - DjangoHaesView Answer on Stackoverflow
Solution 2 - Djangotshubham7View Answer on Stackoverflow
Solution 3 - DjangoVitthal SarodeView Answer on Stackoverflow
Solution 4 - DjangoAdrian StanicaView Answer on Stackoverflow
Solution 5 - DjangoSincere_YeView Answer on Stackoverflow