我正在开发一个Django博客,我想在表单中使用布尔字段
这是我的代码:
status = forms.CharField(widget=forms.Select(choices=(('1', 'Active'),('0', 'Inactive'),), attrs={'class':'form-control'}))
我想更改状态字段
par(mfrow = c(3, 3))
map2(forecasts, names(forecasts),
~ plot( .x,
main = .y,
bty = "n",
ylab = "Monthly Revenue",
xlab = "Time"))
# same as map2 but returns nothing
# suitable for plotting & writing output to files
walk2(forecasts, names(forecasts),
~ plot( .x,
main = .y,
bty = "n",
ylab = "Monthly Revenue",
xlab = "Time"))
pwalk(list(forecasts, names(forecasts)),
~ plot( ..1,
main = ..2,
bty = "n",
ylab = "GDP",
xlab = "Year"))
# to save some typing
iwalk(forecasts,
~ plot( .x,
main = .y,
bty = "n",
ylab = "Monthly Revenue",
xlab = "Time"))
使用布尔字段函数(True或False)
我该怎么办?谢谢
答案 0 :(得分:1)
您可以选择某个值来表示True
和False
(使用字符串),然后使用函数“强制”(例如通过 lambda表达式):
status = forms.TypedChoiceField(
choices=((True, 'Active'), (False, 'Inactive')),
coerce=lambda x: x == 'True',
)
我们因此使用值True
和False
,之后我们使用coerce
将字符串表示返回转换为有效的布尔值。