Retrieve related field in another table

3

I am using django 1.10, with python 3.5 in Windows 8.1. I have a database that considers fields on an element (Work), in another table (Modality) I have the identifier and the description of this Modality, but in Work, I only have saved the value of the modality.

I already have the model that displays the Work elements, but it shows me the Modality value, when I want to see the description. The models are:

class Modalidad(models.Model):
    MODALIDAD_CHOICES = (
        ('1', 'PechaKucha'),
        ('2', 'Cartel'),
    )
    descripcion = models.CharField(max_length=1, choices= MODALIDAD_CHOICES)

    def __str__(self):
        return  self.descripcion

class Trabajo(models.Model):
    no_trabajo = models.CharField(max_length=4)
    modalidad = models.ForeignKey(Modalidad)
    titulo = models.CharField(max_length=200)
    area_tematica = models.ForeignKey(AreaTematica)
    autores = models.ManyToManyField(Autor)
    facultad = models.CharField(max_length=100)
    institucion = models.CharField(max_length=150)
    pais = models.CharField(max_length=20)

    def __str__(self):
        return self.titulo

The view I use is the following:

from trabajo.models import Trabajo    

def carga_trabajos(request):
        lista_trabajos = Trabajo.objects.all()
        return render(request, 'carga_trabajos.html', {'lista_trabajos' : lista_trabajos})

and in the html document, I use a table to display the list:

<tbody>
       {% for dato in lista_trabajos %}
       <tr>
           <td>{{ dato.no_trabajo }}</td>
           <td>{{ dato.titulo }}</td>
           <td>{{ dato.modalidad }}</td>
           <td>{{ dato.facultad }}</td>
           <td>{{ dato.institucion }}</td>
           <td>{{ dato.pais }}</td>
        </tr>
        {% endfor %}
 </tbody>

I know that in the view I would have to adjust the code to include Modalidad.descripcion so that I can include it in the table.

And as you can see, in the same way I have to adjust the code to recover Area_tematica.descripcion and visualize it in my table, but with the help of the first one, I could solve the second one.

I thank you very much for your support. I am very excited using django since with this framework I am getting very clear about how to work for the development of applications.

Everyone greetings. Gustavo.

    
asked by Gustux 01.06.2016 в 01:23
source

2 answers

2

You could create a view in the database to which Django is connected that already has the join already made between the tables and show it directly, I do not know what basis you will be working with but if it is a relational one for example you could run something So in the base:

CREATE VIEW [Trabajo_Modalidad]
AS
SELECT    no_trabajo, titulo, modalidad_descricpcion, facultad, institucion, pais
FROM       trabajo inner join modalidad 
    on trabajo.modalidad_id=modalidad.modalidad_id

Considering that work has the fields that you show in the doc html, and the modality table has the fields (modal_id, modal_descripcion)

    
answered by 01.06.2016 в 01:42
1

First of all, consider the need to have a model that has only two fixed options. It does not seem so efficient to have a table in the database and make related queries if these may well be static options.

Look, this example:

FOO = 1
BOO = 2

MODALIDADES =(
    (FOO, 'PechaKucha'),
    (BOO, 'Cartel'),
)

class Trabajo(models.Model):
    no_trabajo = models.CharField(max_length=4)
    modalidad = models.CharField(max_length=1, choices=MODALIDADES)
    titulo = models.CharField(max_length=200)
    area_tematica = models.ForeignKey(AreaTematica)
    autores = models.ManyToManyField(Autor)
    facultad = models.CharField(max_length=100)
    institucion = models.CharField(max_length=150)
    pais = models.CharField(max_length=20)

    def __str__(self):
        return self.titulo

Now, what you are trying to do is resolved with the Modelo.get__FOO__display() method that Django provides for each element in a field with options choices , where FOO is the name of the field and the result is the value < em> readable of the selected option.

In your example, it would be 'Work.:

<tbody>
  {% for dato in lista_trabajos %}
  <tr>
    <td>{{ dato.no_trabajo }}</td>
    <td>{{ dato.titulo }}</td>
    <td>{{ dato.get_modalidad_display }}</td>
    <td>{{ dato.facultad }}</td>
    <td>{{ dato.institucion }}</td>
    <td>{{ dato.pais }}</td>
  </tr>
  {% endfor %}
</tbody>

The Django documentation has an example in this sense: Model. get_FOO_display () .

    
answered by 01.06.2016 в 02:24