SQL query with 3 tables

Yes, that would be the easy way of doing it. Believe me, I would be spending a lot less time now if I just posted the answer.

But this question is highlighting a more fundamental issue that is important for you to understand.

You wrote:

Absolutely - and that is my objective as well - to help you become a better programmer.

Part of that is to get you to think about the objects that you are working with, and to focus on the data types of those objects.

Actually this statement is incorrect.

I asked you a specific question - what is the data type of the sessao object that you are supplying in your context? (Or, the same question phrased differently, what is the data type that that query returns?)

If you are unsure, this is something you can very easily determine yourself by using the Django shell.

It’s a queryset with two records

Yes.

So lets go back to some of where we started - see the reply and response at SQL query with 3 tables - #3 by mfreitas64

You had two parts to that template:

and

At that point, you were using a get and not filter.

Since the get returns an instance and not a queryset, the first line would have been correct, but the second section had to be reworked to work with an individual object.

So you made the change to the second, but then at SQL query with 3 tables - #10 by mfreitas64 you clarified that you actually do want a queryset - that there are multiple items to be retrieved.

We worked through that to SQL query with 3 tables - #18 by mfreitas64, where you are now handling the second part correctly.

But your first part is still looking to use sessao as if it were a single object - but it’s not.

The sessao object in the context is a queryset. You’ve got exactly the same situation with that top part as you have with the second part.

You don’t have a single sessao from which to populate
<h4> {{ sessao.classeid.turma_name }} - {{ sessao.classeid.weekdays }} - {{ sessao.classeid.times }}</h4>, you have an indeterminate number of such instances in the queryset (currently 2).

What you need to decide is what you want to render at that location. Do you want to pick the first entry in the queryset? The last? Render all of them? Do something else?

The key takeaway from this is that when you see something like sessao in your template, your first thought should be “What is this object and what attributes does it have?” Or, from a slightly different perspective, “Is this object what I think it is?”.

This is particularly important with templates is because if you reference an attribute that doesn’t exist in a template, there’s no error thrown - the rendering engine doesn’t render anything for it.

Some people call this a weakness of the template render engine, but I consider it a particular strength - it makes it easier to build more generic templates and only supply the data in the context that you need to use. Otherwise, you’d need to fill your context with a set of placeholders for every variable being used in a template.

I managed to figure it out.
Thanks for your patience.
Regards