Solution 1 :

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.

Solution 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 %}

Problem :

I want to filter a queryset that depends on another queryset that already depends on another queryset

My models.py

class Escola(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)
    nome = models.CharField(db_column='Nome', max_length=255, blank=True, null=True)

class Inscrio(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True)
    escolaid = models.ForeignKey(Escola, models.DO_NOTHING, db_column='EscolaID', blank=True, null=True)

class Utilizador(AbstractBaseUser)
    id = models.AutoField(db_column='ID', primary_key=True)
    inscriçãoid = models.ForeignKey(Inscrio, models.DO_NOTHING, db_column='InscriçãoID', blank=True, null=True)
    email = models.CharField(db_column='Email', max_length=255, blank=True, null=True, unique=True) 
    nome = models.CharField(db_column='Nome', max_length=255, blank=True, null=True)
    password = models.CharField(db_column='Password', max_length=500, default='pass');

USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'

My views.py

def view_forms(request):
   return render(request, 
                 "main/view_forms.html", 
                 {"escolas": Escola.objects.all(),
                 })

I am doing

{% for escola in escolas %}
   {% for inscrio in escola.inscrio_set.all %}
        {% for utilizador in inscrio.utilizador_set.all %}
            <tr>
            <td><center>{{inscrio.id}}</center></td>
            <td><center>{{escola.nome}}</center></td>
            <td><center>{{utilizador.id}}</center></td> 
        {% endfor %}
    {% endfor %}
{% endfor %}

I am currently trying to get the Inscrio data from Escola.
But when I try to get the Utlizador data from the Inscrio I get nothing.

How can I do this?

Thanks in advance

Comments

Comment posted by Arthur

thats only printing the last one (“{{utilizador.id}}”). doesnt print the other ones

Comment posted by Michael Hawkins

Make sure the data in your database has those foreign key fields populated. As shown, it should print all of the data if it is present. I noticed you allow them to be blank and null.

Comment posted by Arthur

yep, I am sure they are populated

Comment posted by Michael Hawkins

You have it in your template EXACTLY as above?

Comment posted by Michael Hawkins

Can you print the contents of the “utilizadores” object from my code in your console and show me what it looks like?