Django Models无线电输入 [英] Django Models Radio Input
本文介绍了Django Models无线电输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在表单中加入单选按钮.在我的 forms.py 中,该表单具有以下字段:
I am trying to incorporate radio buttons in my form. In my forms.py I have the following fields for the form :
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'gender']
在我的 models.py 中:
user = models.ForeignKey(User, db_column='user_id')
first_name = models.CharField(max_length=250)
last_name = models.CharField(max_length=250
GENDER = (('M', 'Male'), ('F', 'Female'), ('O', 'Other'))
gender = models.CharField(max_length=1, choices=GENDER, null=True)
我希望将 gender
呈现为单选按钮,而不是CharField.但是我知道模型模块不支持RadioSelect,并且我也不能使用小部件.有办法吗?
I want gender
to be rendered as a radio button not a CharField. But I know the models module do not support the RadioSelect and I cannot use widget either. Is there a way to do this?
推荐答案
我不知道您为什么说也不能使用小部件".当然可以,以Meta类的形式:
I don't know why you say you "cannot use widgets either". Of course you can, in the form Meta class:
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'gender']
widgets = {'gender': forms.RadioInput}
这篇关于Django Models无线电输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文