django源码解析之 BooleanField (二)
class BooleanField(Field):
empty_strings_allowed = False
default_error_messages = {
'invalid': _(u"'%s' value must be either True or False."),
}
description = _("Boolean (Either True or False)") def __init__(self, *args, **kwargs):
kwargs['blank'] = True
if 'default' not in kwargs and not kwargs.get('null'):
kwargs['default'] = False
Field.__init__(self, *args, **kwargs) def get_internal_type(self):
return "BooleanField" def to_python(self, value):
if value in (True, False):
# if value is 1 or 0 than it's equal to True or False, but we want
# to return a true bool for semantic reasons.
return bool(value)
if value in ('t', 'True', ''):
return True
if value in ('f', 'False', ''):
return False
msg = self.error_messages['invalid'] % str(value)
raise exceptions.ValidationError(msg) def get_prep_lookup(self, lookup_type, value):
# Special-case handling for filters coming from a Web request (e.g. the
# admin interface). Only works for scalar values (not lists). If you're
# passing in a list, you might as well make things the right type when
# constructing the list.
if value in ('', ''):
value = bool(int(value))
return super(BooleanField, self).get_prep_lookup(lookup_type, value) def get_prep_value(self, value):
if value is None:
return None
return bool(value) def formfield(self, **kwargs):
# Unlike most fields, BooleanField figures out include_blank from
# self.null instead of self.blank.
if self.choices:
include_blank = (self.null or
not (self.has_default() or 'initial' in kwargs))
defaults = {'choices': self.get_choices(
include_blank=include_blank)}
else:
defaults = {'form_class': forms.BooleanField}
defaults.update(kwargs)
return super(BooleanField, self).formfield(**defaults)
看起来,BooleanField 要比复杂的多,我们只分析其中的
to_python 函数
def to_python(self, value):
if value in (True, False):
# if value is 1 or 0 than it's equal to True or False, but we want
# to return a true bool for semantic reasons.
return bool(value)
if value in ('t', 'True', ''):
return True
if value in ('f', 'False', ''):
return False
msg = self.error_messages['invalid'] % str(value)
raise exceptions.ValidationError(msg)
函数获得一个参数value,判断value是不是 (True,False,1, 0)中的一个,如果是,返回True或False。
下面同理,在value是字符串的情况下,判断value的值 是不是 ('t', 'True', '1') 中的一个,是则返回 True...
如果执行到msg = XXXXX 这里,就说明 to_python执行失败了,返回错误...抛出异常...
需要注意的是:
>>> a = True
>>> b = False
>>> c = 1
>>> d = 0
>>> e = 11
>>> f = -1
>>> a in (True,False)
True
>>> b in (True,False)
True
>>> c in (True,False)
True
>>> d in (True,False)
True
>>> e in (True,False)
False
>>> f in (True,False)
False
>>>
如果一个大于1的数,是不会 in (True,False) 中的,这和我们平时使用
>>> if 11:
print 'aa' aa
>>>
是不同的。
django源码解析之 BooleanField (二)的更多相关文章
- django源码解析之 BooleanField (三)
def __init__(self, *args, **kwargs): kwargs['blank'] = True if 'default' not in kwargs and not kwarg ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- [spring源码] 小白级别的源码解析ioc(二)
之前一篇,整体描述了一下 Spring的整体概况和 jar包的介绍. 现在开始进入具体的源码解析,从本篇开始,先介绍spring的ioc容器.之前也看过一些介绍spring源码的, 有的是只讲整体的接 ...
- JDK8源码解析 -- HashMap(二)
在上一篇JDK8源码解析 -- HashMap(一)的博客中关于HashMap的重要知识点已经讲了差不多了,还有一些内容我会在今天这篇博客中说说,同时我也会把一些我不懂的问题抛出来,希望看到我这篇博客 ...
- MyBatis源码解析(十二)——binding绑定模块之MapperRegisty
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6758456.html 1.回顾 之前解析了解析模块parsing,其实所谓的解析模块就是为 ...
- mybatis源码-解析配置文件(二)之解析的流程
目录 1. 简介 2. 配置文件解析流程分析 2.1 调用 2.2 解析的目的 2.3 XML 解析流程 2.3.1 build(parser) 2.3.2 new XMLConfigBuilder( ...
- django源码解析之BigIntegerField (一)
要分析django的源码,来更深入的学习django,是一个不错的方法,可惜需要大量的时间. 所以,能分析多少就是多少吧. 本次源码分析以1.4.16为基础. 用sublime 打开下载的源码,使用 ...
- Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?
不知道一些同学有没有这种疑问,为什么Mybtis中要配置dataSource,Spring的事务中也要配置dataSource?那么Mybatis和Spring事务中用的Connection是同一个吗 ...
- bitcoin 源码解析 - 交易 Transaction(二) - 原理篇
这篇文章我断断续续写了呃···· 应该快三个星期了? 所以前后的风格可能差别相当大.真是十分的怠惰啊··· 最近实在是不够努力.用python重写bitcoin的项目也卡在网络编程部分(这方面真是我的 ...
随机推荐
- 【Android】详解Android的menu菜单
在软件应用过程中,菜单的存在是必不可少的,我这次讲一下,我对android菜单的一个基础做法吧 Android的菜单分为三种类型:选项菜单(Option Menu).上下文菜单(Context Men ...
- struts2 标签变形和 样式class无效 问题解决方法
在jsp使用Struts2标签的时候会发现,出现严重变形问题. <s:textfield type="text" name="username" labe ...
- Spark日志清洗
日志数据清洗,主要采用spark 的定时任务,清洗出有效数据,并保存到hive数据仓库中存储.常用流程如下: 参考:https://gaojianhua.gitbooks.io/bigdata-wik ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
- JDBC连接SQLServer出现的异常
数据库连接. question1. java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver 异常 ...
- mvc 缓存页面 减轻服务器压力
方法: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- 使用grep恢复被删文件内容
在Unix/Linux下,最危险的命令恐怕就属rm命令了,每次在root下使用这个命令的时候,我都要盯着命令行看上几分钟才敢把回车敲下去.以前,看到同事在脚本中使用rm命令 —— rm {$App_D ...
- ios标准开发者账号 ios企业开发者账号的区别总结
ios标准开发者账号 ios企业开发者账号的区别总结 ios标准开发者项目 1.ios标准开发者项目账号可以发布到app store 2.ios标准开发者项目分为两种:①个人开发者②公司/机构开发 ...
- debian8最小化安装,字符界面的中文配置
一.现象: debian8最小化安装以后,字符界面的中文显示乱码. 二.解决 1. 安装locales apt-get install locales 2. 配置locales dpkg-reconf ...
- .net自定义控件Control、WebControl、CompositeControl
一.呈现方法 1.Control主要有以下4个方法用于呈现 //该方法为入口方法 public virtual void RenderControl (HtmlTextWriter writer) { ...