Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a Django URLField has fixed max_length as 200 characters

Tags:

python

django

link = models.URLField(max_length=500, blank=True, default='')

I have already set the max_length as 500, but every time I tried to set this field with url larger than 200 characters, it still threw an error saying that Ensure this value has at most 200 characters (it has 327). Is this a Django restriction or something I forgot to set up?

like image 446
chaonextdoor Avatar asked Apr 18 '14 21:04

chaonextdoor


1 Answers

I guess the problem is solved by now, but I ran into the same issue last week. While the answers regarding migrations are correct, it is worth noting that URLField is based on CharField. This means that the length limitations of CharField also apply to URLField, no matter what max_length you define (see also https://docs.djangoproject.com/en/3.0/ref/models/fields/#urlfield - this answer referred to Django 1.9 which was the latest release when I initially wrote it and this is still the same for Django 3.0 in 2020).

Somewhat unrelated to the original question, but a possible problem even if you use the default max_length=200 is how MySQL currently deals with UTF-8 text. You can find out more about this VARCHAR() indexing issue on https://serversforhackers.com/mysql-utf8-and-indexing or in the MySQL 5.7 manual on http://dev.mysql.com/doc/refman/5.7/en/charset-unicode-upgrading.html.

A possible solution for having a longer URLField has been posted in Advantages to using URLField over TextField? - simply define a Textfield with the relevant validator.

like image 82
goetz Avatar answered Oct 12 '22 22:10

goetz