Obtain the object of a foreign key in Django

1

I have these models:

class Direccion(models.Model):
    pais = models.CharField(max_length=30)
    estado = models.CharField(max_length=30)
    municipio = models.CharField(max_length=30)
    ciudad = models.CharField(max_length=40, null=True, blank=True)
    calle = models.CharField(max_length=40)
    colonia = models.CharField(max_length=30, null=True, blank=True)
    numero_interior = models.IntegerField(null=True, blank=True)
    numero_exterior = models.CharField(max_length=5, null=True, blank=True)
    codigo_postal = models.IntegerField()
    datos_adicionales = models.CharField(max_length=120, null=True, blank=True)


class Empleado(models.Model): 
    direccion = models.ForeignKey(Direccion, null=True, blank=True)
    nombre = models.CharField(max_length=80)
    apellido_paterno = models.CharField(max_length=80)
    apellido_materno = models.CharField(max_length=80, blank=True, null=True)
    rfc = models.CharField(max_length=18)
    curp = models.CharField(max_length=18)
    fecha_nacimiento = models.DateField(null=True, blank=True)
    fecha_registro = models.DateTimeField(auto_now_add=True)
    activo = models.IntegerField(default=1, editable=False)

And I call all the attributes of the model Empleado of a table in my template, but inside a cell I want to call all the attributes of the address corresponding to each employee and concatenate them, I was looking for how to do it but not I find something close to what I want to do.

For now I'm just returning the values of the model Empleado and the ID of the address like this:

class EmpleadosList(ListView):
    model = Empleado

    def get_queryset(self):
        queryset = Empleado.objects.filter(activo=1).order_by('id')
        return queryset

And in the template:

{% 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_id }}</td>

    </tr>
{% endfor %}
    
asked by Angie Alejo 17.01.2016 в 03:14
source

1 answer

4

It is possible to call within your template the addresses related to the employee:

{% 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>
        {% for direccion in empleado.direccion_set.all %}
            <td>{{ direccion.pais }}</td>
            <td>{{ direccion.estado }}</td>
            <td>...</td>
        {% empty %}
            <td>!El empleado no tiene direcciones!</td>
        {% endfor %}
    </tr>
{% endfor %}

Although I recommend you use prefetch_related to get directions related in a single block.

Also, I have some recommendations about your models:

  • The field activo of your model Empleado , should be of type BooleanField , it does not make sense for you to use an integer to represent a Boolean value:

    activo = models.BooleanField(default=True, editable=False)
    
  • In the event that an employee has only one address, you should use a relationship OneToOneField :

    direccion = models.OneToOneField(Direccion, null=True, blank=True)
    
answered by 17.01.2016 / 03:38
source