Because the “Post” model has a relation with the “Category” model so you can access its fields.
Note the “double underscore” here category__category_name
Side note: When posting code, templates, error messages, etc here, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (templates, etc), then another line of ```. This forces the forum software to keep your code properly formatted.
You’re directly referencing the related object manager “all” here - this is going to issue a query to retrieve all related Post. (Most importantly, it’s not going to use the other query you’ve defined on Post.)
There are a couple different ways to address this. It’s up to you to decide which approach works best for you.
You can:
Create a model method in Category to retrieve the latest three Post, and iterate over it in your template.
If you’re using PostgreSQL, you could use the ArrayAgg function to annotate the list of the latest three Post in Category, and iterate over that list
Perform your query on Post and reorganize the data within your view before sending it to the template.
The text and examples at Extra instance methods. (I’m providing this link only for additional examples and ideas, not that you need to implement these specific methods.
Give it a try, see what you come up with, and feel free to ask further questions if you need more help.