为什么Django的URLField默认截断为200个字符? [英] Why does Django's URLField truncate to 200 characters by default?

查看:49
本文介绍了为什么Django的URLField默认截断为200个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢Django,并定期使用它.我发现它的大多数默认设置都是理智的,但是人们总是困扰我,以至于我在每个项目中都将其覆盖.

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.

URLField 模型字段为200个字符.文档验证了此限制,但没有解释为什么会出现这种情况.

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

我将其覆盖,因为很多URL的长度超过200个字符,尤其是带有查询字符串的情况.

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

我了解尽管HTTP规范至少2000个字符,我建议使用新的默认设置.

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.

出于好奇的考虑,我在2007年1月发现了第一次提交添加限制的提交(@jacobian编写的# f6390e8 );从那以后基本上没有改变.

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

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

再回头,我发现我认为是将字段类型映射为ORM(@adrianholovaty的#f69cf70 )的数据库列类型的代码版本.例如,MySQL的文件存在于2006年5月,与Django 0.95相同,限制为200个字符:

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

推荐答案

我不能说为什么选择原始最大长度,您必须问问找到的补丁的作者.请注意, MySQL会将唯一的var字符限制为255个字符,因此选择200可以避免此问题.

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.

选择限制后,对其进行更改将存在向后兼容性问题.由于迁移是在Django 1.7中添加的,因此进行这样的更改会更容易.如果您强烈希望更改它,那么与堆栈溢出相比,Django开发人员邮件列表或票务跟踪器可能是一个更好的询问场所.

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.

您可能会发现有关机票19515 的讨论很有趣.

You might find the discussion on ticket 19515 interesting.

这篇关于为什么Django的URLField默认截断为200个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆