Django BooleanField作为单选按钮? [英] Django BooleanField as radio buttons?

查看:406
本文介绍了Django BooleanField作为单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django 1.0.2中有一个小部件将一个 models.BooleanField 作为两个单选按钮而不是复选框?

Is there a widget in Django 1.0.2 to render a models.BooleanField as two radio buttons instead of a checkbox?

推荐答案

您可以通过覆盖ModelForm中的字段定义来实现:

You could do this by overriding the field definition in the ModelForm:

class MyModelForm(forms.ModelForm):
    boolfield = forms.TypedChoiceField(
                   coerce=lambda x: x == 'True',
                   choices=((False, 'False'), (True, 'True')),
                   widget=forms.RadioSelect
                )

    class Meta:
         model = MyModel

这篇关于Django BooleanField作为单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆