在TextField上使用URLField的优点? [英] Advantages to using URLField over TextField?

查看:141
本文介绍了在TextField上使用URLField的优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,当您使用PostgreSQL数据库时,应始终使用 TextField 作为变长字符串,因为 TextField之间的速度差异对于PostgreSQL, CharField 可以忽略不计。我相对较新的Django,并且正在考虑在我的数据库中使用一个 TextField 用于可变长度的URL。我想知道使用 URLField 有什么好处吗?对于网址使用 TextField 而不是 URLField 会被认为是错误的形式?

解决方案

URLField实际上是CharField支持基于Regexp的URL模式检查和在线验证器(这是无用的,很快就会被弃用),你可以如果您不关心URL的长度限制,请使用TextField

  from django.core.validators import URLValidator 

#在模型
field = models.TextField(validators = [URLValidator()])

此外,使用CharField或TextField取决于是否要对字段进行最大长度约束,哪种元素类型更适合编辑:textarea或input。在PostgreSQL方面,没有显着差异。


As I understand it you should always use a TextField for a variable length string when your using a PostgreSQL database because the speed difference between a TextField and a CharField is negligible with PostgreSQL. I'm relativly new to Django, and was considering using a TextField for variable length urls in my database. I was wondering if there are any advantages to using the URLField? Would it be considered bad form to use a TextField rather than a URLField for urls?

解决方案

URLField is actually CharField w/ supporting of Regexp-based URL pattern checking and a online validator(which is useless and going to be deprecated soon), you could use TextField if you don't care length-limitation of URL

from django.core.validators import URLValidator

# in model
field = models.TextField(validators=[URLValidator()])

Furthermore, using of CharField or TextField depends on whether you want max-length constraint on the field, and which element type is more suitable for editing: textarea or input. On PostgreSQL side, there is no significant difference.

这篇关于在TextField上使用URLField的优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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