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.