Как получить объекты 1 таблицы, связанные с 2 ForeignKey?

<

presupuestos.html

<tbody>
   {% for pago in pagos %}
   <tr>    
      <td>
         {% for presupuesto in presupuestos %}
         {{presupuesto.cliente.nombre}}
         {% endfor %}
      </td>
      <td>
         {{pago.cantidad_pagada}}
      </td>
   </tr>
   {% endfor%}
</tbody>

pagos/models.py

class Pagos(models.Model):
    numero_transaccion=models.IntegerField()
    estimate=models.ForeignKey(Presupuestos, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return f'{self.numero_transaccion}' 

presupuestos/models.py

class Presupuestos(models.Model):
    cliente= models.ForeignKey(Clientes, on_delete=models.SET_NULL, null=True)
 

    def __str__(self):
        return f'{self.cliente}'

clientes/models.py

class Clientes(models.Model):
   
    nombre = models.CharField(max_length=200, blank=True)
   

    def __str__(self):
        return f'{self.nombre}'

views.py

def presupuestosIndex(request):

    presupuestos = Presupuestos.objects.all()
    pagos=Pagos.objects.all()
    


    return render(request, "Presupuestos/presupuestos.html", {'presupuestos':presupuestos,'pagos':pagos})


У вас есть внешний ключ estimate от Pagos к Presupuestos. Если это то отношение, которое вы хотите отобразить для каждого pago, то вы сделаете

<tbody>
   {% for pago in pagos %}
   <tr>    
   <td>         
     {{pago.estimate.cliente.nombre}}
   </td>
   <td>
     {{pago.cantidad_pagada}} <! not in the models in the question -->
   </td>
   </tr>
   {% endfor%}
</tbody>
Вернуться на верх