views:

50

answers:

2

Hi guys,

I have the following in my django model:

PRIORITY = (
    (1, 'Low'),
    (2, 'Normal'),
    (3, 'High'),
)

Obviously the entry associated with this is storing the integer. In my template however I would like to show the priority in human-readable format. How exactly do I accomplish this?

My template:

{% for x in items %}
{{ x }} (added on {{ x.create_date }})<br>
{% endfor %}

{{ x.id }} would be the priority ID.

Thanks in advance.

A: 

Really, the "priority" with name and ID is a self-invented kind of object that you've thought up. If you just make this a Priority model and treat it as such it will all work the way it should. It's because you're trying to avoid using the system that you're having trouble.

Teddy
I'm still learning django, so learning mostly by example. How would I go about achieving this?
Marc
+4  A: 

Assuming you have correctly set the choices option when defining your model, Django automatically creates helper functions to display the names for you. See the documentation on extra instance methods for details.

If your model instance is x and your attribute that stores the priority is priority, then in your template you would use:

{{ x.get_priority_display }}
Wogan
this is the right answer!
Guy Bowden