Why does Django's URLField truncate to 200 characters by default?

一曲冷凌霜 提交于 2021-01-03 06:36:50

问题


I'm fond of Django and use it regularly. I find most of its defaults sane, but one has always bothered me to the point that I override it on every project.

The default max length of a URLField model field is 200 characters. The docs verify this limit, but don't explain why it is the case.

class URLField(CharField):
    ...

    def __init__(self, verbose_name=None, name=None, **kwargs):
        kwargs['max_length'] = kwargs.get('max_length', 200)
        ...

I override it because plenty of URLs are longer than 200 chars, especially with a query string.

I understand that though the HTTP spec does not specify a maximum length, virtually all browsers support at least 2000 characters, which to me suggests a new sane default.

Curious to dig deeper, I found the first commit to add the limit in January 2007 (#f6390e8 by @jacobian); it's been essentially unchanged since.

https://github.com/django/django/blob/f6390e8983dd364053078cc80361cb369b667690/django/db/models/fields/init.py#L805

Going back even further, I discovered what I think is the version of the code mapping field types to database column types for the ORM (#f69cf70 by @adrianholovaty). The file for MySQL for example exists in May 2006 with the same 200-character limit since Django 0.95:

https://github.com/django/django/blob/f69cf70ed813a8cd7e1f963a14ae39103e8d5265/django/db/backends/mysql/creation.py#L28


回答1:


I can't say why the original max length was chosen, you would have to ask the authors of the patches you found. Note that MySQL restricts unique var chars to 255 characters, so choosing 200 avoids this problem.

Once the limit was chosen, changing it would have backwards compatibility issues. Since migrations were added in Django 1.7, it would be easier to make a change like this. If you feel strongly about changing it, the Django developers mailing list or ticket tracker is probably a better place to ask than stack overflow.

You might find the discussion on ticket 19515 interesting.



来源:https://stackoverflow.com/questions/34214037/why-does-djangos-urlfield-truncate-to-200-characters-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!