Thursday, August 31, 2023

Re: Django Resolve Choices Class

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:

Regards,

Andréas


Den ons 30 aug. 2023 kl 22:11 skrev sebasti...@gmail.com <sebastian.jung2@gmail.com>:
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/53c122e7-0448-4d67-9cd4-ce11ffa2ef5cn%40googlegroups.com.

--
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 django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCcCrg-VtCdJDFRicqY0WpvNw28SMRR3tAzvaUro69gurw%40mail.gmail.com.

No comments:

Post a Comment