何时在Django中使用NullBooleanField [英] When to use NullBooleanField in Django
问题描述
我有一个按钮,当单击该按钮时,应该将用户喝过水的情况保存在数据库中。我只是想检查NullBooleanField是否将是定义此方法的正确方法。
I have a button that, when clicked, should save in the database that the user has drunk water. I just wanted to check whether NullBooleanField would be the correct way to define this.
一个更广泛的问题是,如果得到回答将对社区有用,则是一个在以下情况下的最佳情况列表使用NullBooleanField。但我在这里不是问这个。以防万一您想要更好的挑战。
A broader question that if answered would be useful to the community is a list of optimal circumstances under which to use NullBooleanField. But I'm not asking that here. Just in case you wanted a better challenge.
谢谢。
推荐答案
您需要回答的问题,以了解是否应使用 BooleanField
或 NullBooleanField
实际上是关于您要在模型中表示的字段的值的可能状态:
The question you need to answer to find out whether you should use the BooleanField
or the NullBooleanField
is actually concerning the possible states of the value of the field you want to represent in your model:
2种可能的状态:
- 用户喝了水
- 用户没有喝水
→使用 BooleanField
3种可能的状态:
- 用户喝了水
- 用户没有喝水
- 不知道用户是否喝过水或没有喝水
→使用 NullBooleanField
。
更新:
NullBooleanField
是在版本3.1中已弃用。而是使用 BooleanField
和 null = True
。
这篇关于何时在Django中使用NullBooleanField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!