Вы бы сделали что-то вроде этого:
game = Game.objects.get(name="Some Game") # Gets the game obj (replace "name" with the identifier you use)
subscriptions = Subscription.objects.filter(game=game) # Filters all the subscriptions associated with the game obj above
sub_count = subscriptions.count() # Uses QuerySet method .count() to get amount of subscription instances, and avoids a potentially costly DB hit
EDIT
Чтобы получить запрос в вашем контексте при использовании ListView
, вы можете сделать что-то вроде этого:
class TitlePostListView(ListView):
model = Post
context_object_name = 'posts'
template_name = 'posts/game.html'
def get_queryset(self):
game = get_object_or_404(Game, slug=self.kwargs.get('slug'))
return Post.objects.filter(game=game).order_by('-date_published')
def get_context_data(self, **kwargs):
game = get_object_or_404(Game, slug=self.kwargs.get('slug'))
context = super(TitlePostListView, self).get_context_data(**kwargs)
context['game'] = game
context['subscription_status'] = subscription_status(self.request.user, context['game'])
context['sub_count'] = Subscription.objects.filter(game=game).count()
return context
И затем в своем шаблоне вы можете использовать {{ sub_count }}
, чтобы увидеть количество подписок для указанной игры.