If you want the string instead of the value, you just use:
entity.get_gender_display()

Django automatically adds these "magic methods" when you have fields that
have choices.

You can see this in the documentation:
https://docs.djangoproject.com/en/4.2/ref/models/fields/#choices

Regards,

Andréas


Den ons 30 aug. 2023 kl 22:11 skrev [email protected] <
[email protected]>:

> I have in my
>
> model.py:
>
> class GendersChoices(models.IntegerChoices):
>     M = 0, _('Male')
>     F = 1, _('Female')
>     N = 2, _('Gender Neutral')
>
> class Address(models.Model):
> gender = models.PositiveSmallIntegerField(choices=GendersChoices.choices,
> blank=True, default=GendersChoices.F, null=True,verbose_name=_('Gender'))
>
> Now i have a entity from Address and when i make entity.gender i get a
> number from database. But i want when in entity are insert 2 then i want to
> get _('Gender Neutral') instead of 2
>
> How i can do that?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/53c122e7-0448-4d67-9cd4-ce11ffa2ef5cn%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/53c122e7-0448-4d67-9cd4-ce11ffa2ef5cn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCcCrg-VtCdJDFRicqY0WpvNw28SMRR3tAzvaUro69gurw%40mail.gmail.com.

Reply via email to