Django - 来自BooleanField的预先选择的单选按钮

时间:2011-09-08 21:52:42

标签: django django-views django-forms

我已经能够从我的主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)
    ....

1 个答案:

答案 0 :(得分:2)

使用PositiveSmallIntegerField设为choicesBooleanField并没有真正为你买任何东西,而且你已经看到它只会给你带来更多麻烦。