Show details in details

1

I have created a form in Django but when I want to show the details of a record it does not bring me the update date, I would like to see why it does not bring me that information.

models.py :

from django.db import models


class Empleados(models.Model):

    OPCIONES_GENERO_CHOICES = (
        ('M', 'Masculino'),
        ('F', 'Femenino'),
    )

    nombre = models.CharField(max_length=15)
    apellidos = models.CharField(max_length=15)
    ci = models.IntegerField(unique=True)
    genero = models.CharField(max_length=255, choices=OPCIONES_GENERO_CHOICES, blank=True, null=True)
    cargo = models.CharField(max_length=15)
    creado = models.DateTimeField(auto_now_add=True)
    email = models.EmailField()
    telefono = models.CharField(max_length=12)
    documento = models.FileField(
        upload_to="archivo/",
        null=True,
        blank=True
    )

    def __str__(self):
        return '%s' % self.nombre

class ActualizacionEmpleado(models.Model):
    empleado = models.ForeignKey('Empleados', null = False, blank = False, on_delete = models.CASCADE)
    fecha_actualizacion = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return '%s' % self.empleado

details.html :

{% extends 'base/base.html' %}

{% block Contenido %}

<div class="container">
<div class="row">
    <div class="col-md-6 col.lg-3">
        <h4>Registro ID #: </h4>
            <ul><p>{{ object.id }}</p></ul>            
        <h4>Nombre: </h4>
            <ul><p>{{ object.nombre }}</p></ul>            
        <h4>Apellidos: </h4>
            <ul><p>{{ object.apellidos }}</p></ul>            
        <h4>Cedula de identidad: </h4>
            <ul><p>{{ object.ci }}</p></ul>
        <h4>Genero: </h4>
            <ul><p>{{ object.genero }}</p></ul>         
        <h4>Fecha de ingreso: </h4>
            <ul><p>{{ object.creado }}</p></ul>            
        <h4>Email: </h4>
            <ul><p>{{ object.email }}</p></ul>            
        <h4>Telefono: </h4>
            <ul><p>{{ object.telefono }}</p></ul>            
        <h4>Documento: </h4>
            <ul><p>{{ object.documento }}</p></ul>
        <h4>Ultima actualizacion: </h4>
            <ul><p>{{ object.actualizacionempleado.fecha_actualizacion }}</p></ul>            
    </div>
</div>
<center>
    <FORM name="buttonbar">
        <INPUT TYPE="button" VALUE="Regresar" onClick="history.back()">
    </FORM>
</center>
<center>
    <input type="button" name="Submit" value="imprimir" onclick="javascript:window.print()"> 
</center>
</div>
{% endblock %}

What do I lack? Because when I call for details, it brings me all the information requested, except for the update date.

    
asked by Jhonny Barreto 22.08.2018 в 19:50
source

2 answers

1

It's because of the way you have the models related.

If you tell your database that in table ActualizacionEmpleado create a foreign key ( ForeignKey ) to the table Empleados . What will be done is that a one-to-many relationship will be created:

ActualizacionEmpleado * => 1 Empleados

From what you see of your Empleados table, you have the inverse relationship, that is, many to one:

Empleados 1 => * ActualizacionEmpleado

In other words, your employee can have many updates (but on models), and your updates only belong to one employee.

Then, saying the above if you execute the following clause: object.actualizacionempleado.fecha_actualizacion , knowing that object is an instance of Empleados , then, according to the previous, Django will not know how to solve that since an employee has many updates, then, which update should it show?

Something to keep in mind, that inverse or relative relation, is assigned an automatic name if you do not determine it by means of the related_field parameter when you define the field in the model. And by default for your case it would be actualizacionempleado_set . With this, it would be right to do this:

object.actualizacionempleado_set.all()

If you see it that way, it's actually a QuerySet as if you were making a query like this: ActualizacionEmpleado.objects.filter(empleado=object)

To help you a little more with your problem, maybe this is not the relationship you need if not one of One to One

But if you want to continue on this path, something like that would help you to at least show something, I would recommend ordering to make sure you have the most updated data: {{ object.actualizacionempleado_set.all.0.fecha_actualizacion }} from Django's template that should show you something as long as you have data in the database

    
answered by 22.08.2018 в 22:45
0

Assuming you want to show the last update date of an employee, you must first perform the search in your view:

actualizacion_empleado = ActualizacionEmpleado.objects().filter(empleado=object).last()

You pass the variable actualizacion_empleado to the template and The change required to show the date would be as follows:

<ul><p>{{ actualizacion_empleado.fecha_actualizacion }}</p></ul>

If your intention is to keep a record of the dates in which the profile of an employee is updated, the way you propose your models is fine, however, consider the auto_now=True option if you only need to save the last date of profile modification Your model would be:

class Empleados(models.Model):

    OPCIONES_GENERO_CHOICES = (
        ('M', 'Masculino'),
        ('F', 'Femenino'),
    )

    nombre = models.CharField(max_length=15)
    apellidos = models.CharField(max_length=15)
    ci = models.IntegerField(unique=True)
    genero = models.CharField(max_length=255, choices=OPCIONES_GENERO_CHOICES, blank=True, null=True)
    cargo = models.CharField(max_length=15)
    creado = models.DateTimeField(auto_now_add=True)
    actualizado = models.DateTimeField(auto_now=True)
    email = models.EmailField()
    telefono = models.CharField(max_length=12)
    documento = models.FileField(
        upload_to="archivo/",
        null=True,
        blank=True
    )

    def __str__(self):
        return '%s' % self.nombre

Thus, each time you edit the employee profile information, the date it was updated will be saved, and in your template you can do the following to show it:

<ul><p>{{ object.actualizado}}</p></ul>
    
answered by 27.08.2018 в 08:50