3

I am trying to use the StatusModel feature of Carl Meyer's awesome django-model-utils package to create a model that has a status field. It's a very nice design in which you subclass your model from StatusModel and pass a Choices object to a field on the model called STATUS, which automatically creates 'status' and 'status_changed' fields on the database representation.

I would like for my status field to have a separate human-readable value than its database representation, and the documentation for the Choices class says that it can be passed a two-tuple in which the first value is the database representation of the choice and the second is the human-readable value. But when I try doing this with my StatusModel using the above Choices object, I still get the database representation when I use the status field in a template.

Here's an excerpt of my model class:

from django.utils.translation import ugettext as _
from model_utils import Choices
from model_utils.models import StatusModel

STATUS_CHOICES = Choices(
    ('awaiting_approval', _('Awaiting approval')), 
    ('returned_to_submitter', _('Returned to submitter')), 
    ('approved', _('Approved')), 
    ('denied', _('Denied')),
)

class Petition(StatusModel):
    STATUS = STATUS_CHOICES
    ...

and here's my template:

<table>
    <tr>
        <th>Status</th>
    </tr>
    {% for petition in petitions %}
    <tr>
        <td>{{ petition.status }}</td> 
        <!-- expecting "Awaiting approval" but it displays "awaiting_approval" -->
    </tr>
    {% endfor %}
</table>

How do I get the model class to return the human readable status? Or does StatusModel not support that feature of the Choices object?

gravelpot
  • 1,677
  • 1
  • 14
  • 20

1 Answers1

6

You can just use the normal get_FOO_display() method - in this case {{ petition.get_status_display }}

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • you can read more in the docs: https://docs.djangoproject.com/en/1.8/ref/models/instances/#django.db.models.Model.get_FOO_display – elad silver May 14 '15 at 14:52