zoukankan      html  css  js  c++  java
  • 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后会出现什么情况,回头试试就知道了。

  • 相关阅读:
    安利博客
    python 的高阶算法之堆排序
    functools模块用途
    类型注解
    高阶函数和装饰器
    生成器

    递归函数
    匿名函数
    函数 的 返回值作用域
  • 原文地址:https://www.cnblogs.com/tk091/p/4082664.html
Copyright © 2011-2022 走看看