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 (二)的更多相关文章

  1. django源码解析之 BooleanField (三)

    def __init__(self, *args, **kwargs): kwargs['blank'] = True if 'default' not in kwargs and not kwarg ...

  2. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  3. [spring源码] 小白级别的源码解析ioc(二)

    之前一篇,整体描述了一下 Spring的整体概况和 jar包的介绍. 现在开始进入具体的源码解析,从本篇开始,先介绍spring的ioc容器.之前也看过一些介绍spring源码的, 有的是只讲整体的接 ...

  4. JDK8源码解析 -- HashMap(二)

    在上一篇JDK8源码解析 -- HashMap(一)的博客中关于HashMap的重要知识点已经讲了差不多了,还有一些内容我会在今天这篇博客中说说,同时我也会把一些我不懂的问题抛出来,希望看到我这篇博客 ...

  5. MyBatis源码解析(十二)——binding绑定模块之MapperRegisty

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6758456.html 1.回顾 之前解析了解析模块parsing,其实所谓的解析模块就是为 ...

  6. mybatis源码-解析配置文件(二)之解析的流程

    目录 1. 简介 2. 配置文件解析流程分析 2.1 调用 2.2 解析的目的 2.3 XML 解析流程 2.3.1 build(parser) 2.3.2 new XMLConfigBuilder( ...

  7. django源码解析之BigIntegerField (一)

    要分析django的源码,来更深入的学习django,是一个不错的方法,可惜需要大量的时间. 所以,能分析多少就是多少吧. 本次源码分析以1.4.16为基础. 用sublime 打开下载的源码,使用 ...

  8. Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?

    不知道一些同学有没有这种疑问,为什么Mybtis中要配置dataSource,Spring的事务中也要配置dataSource?那么Mybatis和Spring事务中用的Connection是同一个吗 ...

  9. bitcoin 源码解析 - 交易 Transaction(二) - 原理篇

    这篇文章我断断续续写了呃···· 应该快三个星期了? 所以前后的风格可能差别相当大.真是十分的怠惰啊··· 最近实在是不够努力.用python重写bitcoin的项目也卡在网络编程部分(这方面真是我的 ...

随机推荐

  1. 树莓派进阶之路 (035) - 基于linux的zsh安装脚本

    基于linux的zsh安装脚本: Ubuntu版本: #!/bin/sh cd #安装zsh sudo apt-get install zsh #查看zsh cat /etc/shells #更改zs ...

  2. Redis学习之路(002)- Ubuntu下redis开放端口

    Redis在ubuntu安装后默认是只有本地访问,需要别的ip访问我们需要修改redis的配置文件 1. dpkg -L redis-server 这命令我们可以看到redis的安装的文件在那些目录 ...

  3. 如何在windows server 2008 部署asp.net mvc

    我们做好的asp.net mvc网站,要部署到windows server 2008(IIS7.0)网站,首先要安装好.net framework 4.0,开发工具VS2010,VS2012,VS20 ...

  4. ios面试题来一波

    一.如果让你实现属性的weak,如何实现的? PS: @property 等同于在.h文件中声明实例变量的get/set方法, 而其中property有一些关键字,其中就包括weak,atomic的. ...

  5. 【Algorithm】自顶向下的归并排序

    一. 算法描述 自顶向下的归并排序:采用分治法进行自顶向下的程序设计方式,分治法的核心思想就是分解.求解.合并. 先将长度为N的无序序列分割平均分割为两段 然后分别对前半段进行归并排序.后半段进行归并 ...

  6. SQL 正则表达式 `(user_log_acct)?+.+`

    SELECT 语句可以使用正则表达式做列选择,下面的语句查询除了 ds 和 hr 之外的所有列: SELECT `(ds|hr)?+.+` FROM test    

  7. [转]PostgreSQL源码结构

    PostgreSQL采用C/S(客户机/服务器)模式结构.应用层通过INET或者Unix Socket利用既定的协议与数据库服务器进行通信. 另外,还有一种‘Standalone Backend’使用 ...

  8. 针对MSHFlexGrid的一系列通用方法-项目中实践代码分享

    1.给MSHFlexGrid填充数据通用方法 '自定义报表填充程序 fgrid Public Function ShowformfData(Resultset As ADODB.Recordset, ...

  9. 各Spring-Boot-Starters介绍(转)

    原文传送门:Spring Boot application starters ↑传送门里有各Spring-Boot-Starters的依赖pom.xml(一般人我不告诉他) 注意:此为初订版,博主渣翻 ...

  10. haproxy 作为反向代理被攻击

    在工作中.遇到过一个情况就是我们的额网站被某一个网站攻击 页面结果来自于https://i.umeng.com/? (友盟+) 我这里用的是haproxy作为反向代理. 所以这里我就从haproxy这 ...