Shubham Devgan
Shubham Devgan

Reputation: 614

Display checkbox value from Database Django

I'm making a ModelForm. I have a Gender field in which I have given choices.In forms.py file I have converted those choices into checkbox using widget.

models .py

class UserModel(models.Model):
    #username=models.ForeignKey(
    #    on_delete=models.CASCADE,
     #   related_name='custom_user')

    name=models.CharField(
        max_length=50,
        verbose_name='Full Name',
    )

    gender=models.CharField(
        choices=(
            ('M','Male'),
            ('F','Female'),
        ),
        max_length=1,
        verbose_name='Gender',
        default='M'
    )

forms.py

class UserForm(forms.ModelForm):


    class Meta:
        model=UserModel
        exclude=()
        widgets = {
            'DOB': forms.SelectDateWidget(),
            'gender':forms.RadioSelect(),
            #'hobbies':forms.CheckboxSelectMultiple()

        }

everything works fine and data is stored in database,But when I fetch data from the database in django shell using commands.Instead of getting the name of choice I get 'M',or 'F'. I know for choice field we can use object_of_modelclass.get_gender_display to get name of choice and it returns full name of choice. But since I have converted the choice to checkbox field in forms.py,now when I fetch data from database using object_of_modelclass.get_gender_display it returns

Django shell commands im using:

from .models import UserModel
a=Usermodel.objects.get(pk=1)
a.get_gender_display

it returns

functools.partial(<bound method Model._get_FIELD_display of <UserModel: hoo>>, field=<django.db.models.fields.CharField: gender>)

How can I get original name as "Male' and 'Female' ? Thanks

Upvotes: 0

Views: 816

Answers (1)

Higor Rossato
Higor Rossato

Reputation: 2046

You should call the get_FIELD_display as a function, therefore using ().

a.get_gender_display()

Upvotes: 2

Related Questions