I'm new to django and I'm having trouble displaying related data from three related databases in a table. I hope you can help me.
Models.py
class recursos (models.Model):
gruporecurso= models.CharField (max_length=50, choices=TIPORECURSO, default=TIPODEFAULT)
descripcion = models.CharField(max_length=100)
estado_disponible =models.BooleanField(default= True)
inventario = models.CharField(max_length=40,blank= True, null=True)
observacion = models.TextField(blank= True, null=True)
def __str__(self):
return self.descripcion
class remito (models.Model):
fecha = models.DateField()
hora = models.TimeField()
responsable = models. ForeignKey(personas, null=True, blank= True, verbose_name='Responsable',on_delete=models.CASCADE)
area_pedido = models.CharField(max_length=20, verbose_name='Área del pedido')
operador = models.CharField(max_length=40, choices=OPERADOR,verbose_name='Entregado por')
lugardeuso = models.CharField(max_length=40,verbose_name='Lugar uso')
observacion = models.TextField(blank= True, null=True)
class detalle_remito (models.Model):
id_remito = models.ForeignKey(remito, null=True, blank=False, on_delete=models.CASCADE)
id_recurso = models.ForeignKey(recursos, null=True, blank=False, on_delete=models.CASCADE)
cantidad = models.IntegerField(default=1)
def __str__(self):
return f' {self.id_recurso.descripcion}'
Views.py
def home (request):
remitos= remito.objects.all()
recursolist= detalle_remito.objects.all()
page= request.GET.get('page',1)
try:
paginator = Paginator(remitos,5)
remitos=paginator.page(page)
except:
raise Http404
return render(request,"RemitoTicapp/home.html",{'entity': remitos ,'recursolist':recursolist,'paginator': paginator})
home.html
{% extends "RemitoTicapp/base.html" %}
{% load static %}
{% block content %}
<!-- Heading
<section >
<div >
</div>
</div>
</section>
-->
<!-- Message -->
<section >
<div >
<div >
<!--<div > -->
<div >
<h2 >
<span >Sistema de Gestión de pedidos</span>
<span > Elementos cedidos</span>
</h2>
<!--<p > -->
<table bg white>
<thead>
<tr>
<th scope="col">Fecha entrega</th>
<th scope="col">Responsable</th>
<th scope="col">Area Pedido</th>
<th scope="col">Lugar de uso</th>
<th scope="col">Recurso / cantidad</th>
<th scope="col">Operador</th>
<th scope="col">Observaciones</th>
</tr>
</thead>
<tbody>
{% for remito in entity %}
<tr>
<td scope="row">{{ remito.fecha }}</td>
<td>{{ remito.responsable }}</td>
<td>{{ remito.area_pedido }}</td>
<td>{{ remito.lugardeuso }}</td>
<td> {% for recurso in recursolis %}
{% if recurso.id_remito == remito.pk %}
<li>{{ recurso.id_recurso.descripcion }} {{recurso.cantidad}}</li>
{% endif %}
{% endfor %}
</td>
<td>{{ remito.operador }}</td>
<td>{{ remito.observacion }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div>
{% include 'RemitoTicapp/paginator.html' %}
</div>
<!-- </p> -->
</div>
</div>
<!-- </div> -->
</div>
</section>
{% endblock%}
We should show the data of Remito with the resources of detalle_remito with the description of each one of them from the recursos table. From already thank you very much
CodePudding user response:
Django will make the reverse foreign key relation accessible with the related model's name followed by a _set
-suffix, exposing an instance of RelatedManager
.
{% for detalle_remito in remito.detalle_remito_set.all %}
{{ detalle_remito.id_recurso.descripcion }}
{% endfor %}
No need to gather addional data for that in your view.
CodePudding user response:
Where you have {% for recurso in recursolis %}, you could instead write {% for recurso in remito.detalle_remito_set.all %} and remove the if statement - it will loop through the detalle_remito objects that are connected to the recurso object