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后会出现什么情况,回头试试就知道了。

相关文章:

  • 2022-01-10
  • 2021-12-25
  • 2021-12-22
  • 2021-09-27
  • 2022-02-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-19
  • 2022-12-23
  • 2022-03-05
  • 2022-01-04
  • 2021-12-06
  • 2021-11-27
  • 2021-11-27
相关资源
相似解决方案