nip3o
nip3o

Reputation: 3467

Use CharField choices not only in admin

I've got a model with a property which represents the current status of something.

STATUSES = (('status1', 'The first status'),('status2', 'The second status'),('status3', 'The third status'))
status = models.CharField(choices=STATUSES)

When using Django-Admin, the choices (i.e. "The first status") is displayed instead of the values (i.e "status1"). How can i achieve this when printing out the status in one of my templates? Or is there any better model field to use in this case?

I know I could just use the same string in both elements in the tuples in STATUSES, but this seems like quite bad practice and makes it hard to rename choices if needed.

Upvotes: 1

Views: 1002

Answers (1)

To display the human readable version of the currently selected choice, use:

{{ instance.get_myfield_display }}

http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display

Upvotes: 3

Related Questions