Home > Back-end >  getting cannot convert to male error in django?
getting cannot convert to male error in django?

Time:01-14

I am trying to export the list of customers using django.

class Gender(models.IntegerChoices):
        FEMALE = 1, female_label
        MALE = 2, male_label

gender = models.PositiveSmallIntegerField(
    db_column="Gender",
    verbose_name=gender_label,
    choices=Gender.choices,
    blank=True,
    null=True,
)

and I am calling in the export row like this:

customer.gender_label

and I am getting such an error Cannot convert 'Male' to Excel

do you have any idea why this error is showing up?

***** update: customer.get_gender_display() solved my problem***

CodePudding user response:

You can get the choice display (so here the value for female_label and male_label) with the .get_gender_display() method, so:

customer.get_gender_display()

Indeed, as the documentation on the .get_fieldname_display() [Django-doc] says:

For every field that has choices set, the object will have a get_FOO_display() method, where FOO is the name of the field. This method returns the “human-readable” value of the field.

  •  Tags:  
  • Related