Show value, not form ID

Asked

Viewed 98 times

1

I’m making a system using Python and Django. At one point, I’m showing a table with database information. Follow my html:

<tbody>
    {% for oportunidade in all_oportunidades %}
    <tr class="clickable-row" data-href="{% url 'crm2:editaroportunidadeview' oportunidade.id %}">
    <td>{{oportunidade.id}}</td>
     <td>{{oportunidade.situacao}}</td>
     <td>{{oportunidade.cliente}}</td>
     <td>{{oportunidade.categoria|default_if_none:""}}</td>
     <td>{{oportunidade.data_abertura|default_if_none:""}}</td>
     <td>{{oportunidade.valor|default_if_none:""}}</td>
     {% endfor %}
     {%else%}
     {%endif%}
     </tbody>

However, in the table, in the fields of situacao and categoria is being shown the IDS of the same and not its content. For example, appears like this in the table;

ID | Cliente | Situação | Categoria
-----------------------------------
 1 | Marcos  |    2     |    1

But I want you to show up like this:

ID | Cliente | Situação | Categoria
-----------------------------------
 1 | Marcos  |Cancelado |  Venda

In models.py, I defined situacao thus:

SITUACAO_OPCOES = (
(u'0', u'Concluída com sucesso'),
(u'1', u'Cancelado'),
(u'2', u'Em andamento')
)

situacao = models.CharField(max_length=1, choices=SITUACAO_OPCOES, default=2)

categoria in the same way.

How do I display the value, not the id?

  • 1

    Just to add some useless information, you don’t have to do the {% if all_oportunidades %}. 'Cause an empty list will cross for and will not do anything at all. : D

1 answer

5


According to the documentation:

For each model field that has Choices set, Django will add a method to Retrieve the Human-readable name for the field’s Current value. See get_FOO_display() in the database API Documentation.

That is, for each field of the model that has the option choices, Django will create a method that will return the human readable value to the current field value, called get_FOO_display(), where FOO will be the name of the field.

So just do it:

oportunidade.get_situacao_display()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.