[Django]-Django How to get a queryset from another queryset in a template

3👍

If I understand what you want, I think you are going about this backwards. Try this:

views.py:

def view_forms(request):
   return render(request, 
                 "main/view_forms.html", 
                 {"utilizadores": Utilizador.objects.all().order_by('inscriçãoid__escolaid__id'),
                 })

In your template:

{% for utilizador in utilizadores %}
        <tr>
        <td><center>{{utilizador.inscriçãoid.id}}</center></td>
        <td><center>{{utilizador.inscriçãoid.escolaid.nome}}</center></td>
        <td><center>{{utilizador.id}}</center></td> 
    {% endfor %}

I will update my answer if this isn’t what you are looking for.

2👍

I guess this is the worst way to do that. You’re trying to render a table with utilizador’s information from all escolas, so get it all and render it!. You can order it by escola id maybe.

query = Utilizador.objects.all().order_by('inscriçãoid__escolaid__id')

template
{% for q in query %}
    <tr>
    <td><center>{{q.inscriçãoid.id}}</center></td>
    <td><center>{{q.inscriçãoid.escola.nome}}</center></td>
    <td><center>{{q.id}}</center></td> 
{% endfor %}

Leave a comment