我已经能够从我的主BooleanField
类中获取Feature
作为其表单的单选按钮,但是在查看时,表单没有预先选择相应的值。在给定布尔值的情况下,如何选择合适的?感谢
models.py:
class Feature(models.Model):
for_biz = models.BooleanField()
class FeatureForm(ModelForm):
choices = ( (1,'Business'), (0, 'Customers') )
for_biz = forms.TypedChoiceField(
coerce=lambda x: bool(int(x)),
choices=choices,
widget=forms.RadioSelect,
)
class Meta:
model = Feature
fields = (
'for_biz',
)
views.py:
def edit_feature(request, f_id):
f = get_object_or_404(Feature, id=f_id)
form = FeatureForm(instance=f)
....
答案 0 :(得分:2)
使用PositiveSmallIntegerField
设为choices
。 BooleanField
并没有真正为你买任何东西,而且你已经看到它只会给你带来更多麻烦。