Django: How can I show the name instead of the id in the list?

Viewed 23

I have a form to create a user, when displaying the data in the list the foreign keys are shown with the id, how can I make the name appear instead of the id?

I'm trying to do it with a for inside the template but it doesn't show me anything

  • my user table has the cargo_empleado table as a foreign key and inside the cargo_empleado table I have a column called nombre_cargo, the column nombre_cargo I want to be displayed instead of the id

template

<td class="ps-0 pe-0">
     {% for cargo_empleado in request.user.cargo_empleado %}
     {% if cargo_empleado.nombre_cargo == 'funcionario' %}
     <a href="" class="btn btn-info btn-sm no-hover" style="cursor: default; border-radius: 2rem; pointer-events: none;">Funcionario</a>
     {% endif %}
     {% if cargo_empleado.nombre_cargo == 'diseñador' %}
     <a href="" class="btn btn-warning btn-sm no-hover" style="cursor: default; border-radius: 2rem; pointer-events: none;">Diseñador</a> 
     {% endif %}
     {% endfor %}

     <!-- {% if display.7 == 1 %}
     <a href="" class="btn btn-info btn-sm no-hover" style="cursor: default; border-radius: 2rem; pointer-events: none;">Funcionario</a> 
     {% else %}
     <a href="" class="btn btn-warning btn-sm no-hover" style="cursor: default; border-radius: 2rem; pointer-events: none;">Diseñador</a> 
     {% endif %} -->
</td>
1 Answers

Django ModelForm calls the model’s __str__ method when rendering ModelChoiceField. You need to customize Your cargo_empleado’s __str__.

Related