Home > database >  How to get the name of IntegerChoices
How to get the name of IntegerChoices

Time:03-13

I have these models.InterChoices

class LogType(models.IntegerChoices):

    SYSTEM_OK = 1
    SYSTEM_REPLY = 2
    SYSTEM_ERROR = 5 

I can get choices for select box such as LogType.choices()

Now, I want to get the name

For example, I want to do this

LogType.get_by_id(1) return SYSTEM_OK or System Ok

is it possible??

CodePudding user response:

You can use the constructor of the LogType, so:

item = LogType(1)

You can then use item.label, or item.name to get the name. For example:

>>> item = LogType(1)
>>> item.label
'System Ok'
>>> item.name
'SYSTEM_OK'
  • Related