I have a model that I have called Empleado
which has a foreign key to a model Direccion
to keep a record of the employee's addresses.
Serializer:
from rest_framework import serializers
from models import Empleado, Direccion
class DireccionSerializer(serializers.ModelSerializer):
class Meta:
model = Direccion
fields = ('id', 'pais', 'estado', 'municipio', 'ciudad', 'calle', 'colonia', 'numero_interior',
'numero_exterior', 'codigo_postal', 'datos_adicionales')
class EmpleadoSerializer(serializers.ModelSerializer):
direccion = DireccionSerializer()
class Meta:
model = Empleado
fields = ('id', 'nombre', 'apellido_paterno', 'apellido_materno', 'fecha_nacimiento', 'rfc', 'curp',
'direccion')
For now I am showing the records of Empleado
in a table with the following cycle:
{% for empleado in object_list %}
<tr>
<td> {{ empleado.id }}</td>
<td> {{ empleado.nombre }} </td>
<td> {{ empleado.apellido_paterno }} </td>
<td> {{ empleado.apellido_materno }} </td>
<td> {{ empleado.fecha_nacimiento|date:"Y-m-d" }} </td>
<td> {{ empleado.curp }} </td>
<td> {{ empleado.rfc }} </td>
<td>
{{ empleado.direccion.get_full_information|truncatechars:30 }}
</td>
</tr>
{% endfor %}
I built the following view:
class EmpleadosListApi(ListAPIView):
serializer_class = EmpleadoSerializer
def get_queryset(self):
return Empleado.objects.filter(activo=1).order_by('id')
And I want to render the JSON in the table instead of using the Django tags. I tried to do it with an example I found on the Internet using the attribute data-field
of Bootstrap but I did not do what I need and I have not found out how to do it. It's the first time I work with APIS, I'm quite new.