Django Radio Models

I am trying to include 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 it to genderappear as a radio button, not a CharField. But I know that the model module does not support RadioSelect, and I also can not use the widget. Is there any way to do this?

+4
source share
1 answer

I don’t know why you say that you can’t use widgets. Of course, you can in the form of a meta class:

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        fields = ['first_name', 'last_name', 'gender']
        widgets = {'gender': forms.RadioInput}
+2
source

Source: https://habr.com/ru/post/1667493/


All Articles