How to expand the model?

Suppose there are models: Match, Player, Team. It is necessary to display in the template all matches with the teams and the composition of the players. The "Match" model automatically reads the Team models using the foreign key and adds them to the QuerySet.

How to add there players and teams (home / guests)? Surely, it is not necessary, when transferring to a template, to separately build a sample by Player with a filter by match ID.

    1 answer 1

    If the team model has a many-to-many relationship to the player model, the problem can be solved using patterns:

    {% extends "base.html" %} {% block content %} {% for match in matches %} {{match}} <br> {% for player in match.home.players.all %} <br> игроок: {{player}} {% endfor %} {% endfor %} {% endblock %} 

    So I understand that you are writing something football on Django? The fact is that I have been engaged in my project for a long time as a hobby. It is dedicated to the courtyard league of my city. If you are interested, you could work together with janga and make a cool project in your portfolio!

    • Michael, thanks for the reply! Naturally, the Team and the Player have a 1-to-many ratio, because one player cannot be listed in other teams. My project is related to American sports, it is also a hobby. Thanks for the suggestion, but for now I’ll work at my own pace. - PythonNewbie
    • While the problem is solved by adding in the subtraction of commands in view, in fact it is equivalent to what you wrote. But it will slow down the response, as it will make several queries to the database. You have to cache the final render in redis. - PythonNewbie