Skip to content
Advertisement

In Django, how can I get a count of records based on a subquery of a subquery?

A baseball player plays in a game if he makes one or more appearances in that game. So, to get a count of the number of games a player played in, you need to count the games that have an inning that have an appearance by that player.

Here are my models:

The SQL query to achieve what I want is:

How could I do this in Django without using Raw SQL?

Note that this is for a PlayerDetailView, so I just need it for a single Player object.

Advertisement

Answer

You can perform such count with:

Where my_player is the player where you want to count the Games for.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement