3

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?

Community
  • 1
  • 1
Houman
  • 64,245
  • 87
  • 278
  • 460

2 Answers2

5

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

context.get_priority_display()
David Pärsson
  • 6,038
  • 2
  • 37
  • 52
-2

The value on the left is the value Django uses to store in the db. It doesn't have to be an integer. Just make both values identical:

PRIORITY = (
        ('high',     _(u'High')),
        ('Medium',   _(u'Medium')),
        ('Low',      _(u'Low')),
    )
TheOne
  • 10,819
  • 20
  • 81
  • 119