0
Save Devs... I need a help to display the fields of a table that is Foreignkey relationship in a Template.
models
class IEIS (models.Model):
nome = models.CharField (max_length=25,null=False, blank=False)
revisao = models.CharField (max_length=20,null=True, blank=True)
pit = models.ForeignKey(PIT, related_name='pit', on_delete=models.CASCADE, verbose_name= 'PIT - Plano de Inspeção e Teste')
class PIT(models.Model):
nome = models.CharField(max_length=50, null=False, blank=False)
fluido = models.ManyToManyField(Fluido, related_name='fluido', blank=True, unique=False)
In the IEIS class I have the PIT field as FK. I need to display in the IEIS view template, some PIT class fields.
views
def view_ieis(request, pk):
ieis = IEIS.objects.get(pk=pk)
especs = ieis.especificacao.all()
fluidos = ieis.fluido.all()
pits = PIT.objects.get(pk=pk)
return render(request, 'ieis/view.html', {'ieis': ieis, 'especs': especs, 'fluidos': fluidos, 'pits': pits,})```
**ieis.html**
PIT
</tr>
</thead>
<tbody>
<tr>
{% for pit in ieis.pits.all %}
{{ pit }} {{ pit.nome }}
{% endfor %}
</tr>
</tbody>
```
Thanks in advance for all the attention, suggestion and help dedicated!
This inversion was at the time to assemble the question. The code is correct. I also cannot understand why it does not return the fields of the PIT table. It takes as object and not the fields.
– Rafael Fidalgo
I updated the answer, maybe it’ll help!
– user247943
It helped yes, thank you. I took a line with your and other suggestions and managed to heal.
– Rafael Fidalgo