12
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?

chaonextdoor
  • 5,019
  • 15
  • 44
  • 61
  • One thing I'd check is the database table's column definition. 200 is the default length, so if you created your database before increasing the max_length it might still be set to 200. – Peter DeGlopper Apr 18 '14 at 21:53

2 Answers2

7

You forgot to actually update/migrate your database. This should happen upon initial syncdb, and ideally via a proper database migration using django-south in Django version up to 1.6, or using the new Django migrations framework to be released in Django 1.7+.

For development purposes you can reset your DB (this will delete all data!) and recreate:

$ ./manage.py reset appname
$ ./manage.py syncdb

If you're using South migrations:

$ ./manage.py schemamigration --auto appname
$ ./manage.py syncdb --migrate
Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
2

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.

goetz
  • 664
  • 10
  • 14