Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

321
Vistas
Get the name or labe from django IntegerChoices providing a valid value

I have django 3.2 and an IntegerChoices class

 class Type(models.IntegerChoices):
        GENERAL = 2, _("general address")
        DELIVERY = 4, _("delivery address")
        BILLING = 6, _("billing address")

I can get the Value name and label easily by doing Type.GENERAL , Type.GENERAL.name and Type.GENERAL.label.

But how can I get these values if I only have the value, e.g. I want to get the Name DELIVERY from the value 4.

Type[4].name ist not working as the list values does not correspond to the int values of the enum.

Thanks Matt

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

IntegerChoices.choices returns you a list of tuples with all content. In your case you'll have something like:

[(2, 'general address'), (4, 'delivery address'), (6, 'billing address')]

Thus it can be done this way:

class Type(models.IntegerChoices):
    GENERAL = 2, _("general address")
    DELIVERY = 4, _("delivery address")
    BILLING = 6, _("billing address")

def label_by_type_value(value):
    choices = [c[1] for c in Type.choices if c[0] == value]
    return choices[0] if len(choices) else None
over 4 years ago · Santiago Trujillo Denunciar

0

You can also do something like this if you can be sure that it is a valid value:

print(Type(2).name)    
print(Type(2).label)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda