Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django/Python: How to cast an integer into the equivalent enum string? [duplicate]

Tags:

python

django

Possible Duplicate:
Django print choices value

In Django in one of the models I have the following enum:

PRIORITY = (
        ('2',     _(u'High')),
        ('1',   _(u'Medium')),
        ('0',      _(u'Low')),
    )

priority = models.CharField(max_length=1, choices=PRIORITY, default='1')

When sending the priority to the template, the value is still in integer, which isn't nice. I would like to show the priority in words rather than digits.

context = Context({'priority':self.priority})

Is there a way to translate priority into the actual string without using any if statements before sending it to the template?

like image 333
Houman Avatar asked Dec 13 '25 11:12

Houman


1 Answers

Yes, according to the documentation you can get the human readable value like this:

context.get_priority_display()
like image 155
David Pärsson Avatar answered Dec 16 '25 07:12

David Pärsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!