问题
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']
In my 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)
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?
回答1:
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}
来源:https://stackoverflow.com/questions/41794352/django-models-radio-input