django源码解析之 BooleanField (三)

1     def __init__(self, *args, **kwargs):
2         kwargs['blank'] = True
3         if 'default' not in kwargs and not kwargs.get('null'):
4             kwargs['default'] = False
5         Field.__init__(self, *args, **kwargs)

在看完 BooleanField 的 to_python 之后,我们来看看 __init__ 

 

先看看我从github上copy的一段代码

    login_required = models.BooleanField(
        _('login required'), default=False,
        help_text=_('Only authenticated users can view the entry.'))

这是一个非常常见的使用 BooleanField 示例。

我们继续看 __init__ 

首先,添加了 blank=True ,然后判断选项中是否设置了 default ,且未添加 null 属性

请看代码

>>> d = {}
>>> d.get('null') == True
False
>>> d = {'null':1234}
>>> d.get('null') == True
False
>>> 

 

至于添加null后会出现什么情况,回头试试就知道了。

posted @ 2014-11-08 02:51  r3call  阅读(3001)  评论(0编辑  收藏  举报