Django 如何计算订户数量

Django 如何计算订户数量,django,Django,我使用这个模型允许用户订阅或取消订阅特定的游戏。但现在我不确定如何计算订阅特定游戏的用户数量 class Subscription(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) game = models.ForeignKey(Game, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_

我使用这个模型允许用户订阅或取消订阅特定的游戏。但现在我不确定如何计算订阅特定游戏的用户数量

class Subscription(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    game = models.ForeignKey(Game, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return str(self.id)
views.py:

class TitlePostListView(ListView):
    model = Post
    context_object_name = 'posts'
    template_name = 'posts/game.html'

    def get_queryset(self):
        title = get_object_or_404(Game, slug=self.kwargs.get('slug'))
        return Post.objects.filter(game=title).order_by('-date_published')

    def get_context_data(self, **kwargs):
        context = super(TitlePostListView, self).get_context_data(**kwargs)
        context['game'] = get_object_or_404(Game, slug=self.kwargs.get('slug'))
        context['subscription_status'] = subscription_status(self.request.user, context['game'])
        return context

你可以按照以下思路做一些事情:

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
编辑

要在使用
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}}
查看指定游戏的订阅计数。

您可以执行以下操作:

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
编辑

要在使用
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}}
查看指定游戏的订阅计数。

每个用户只有一个订阅吗?然后就是Subscription.objects.filter(game=desired\u game.count()。也许这就回答了你的问题:每个用户只有一个订阅吗?然后就是Subscription.objects.filter(game=desired\u game.count()。也许这就回答了你的问题:我完全理解你做了什么。我让它在shell中完美地工作,但是我不确定如何在视图中实现它。我更新了我的帖子以显示我正在尝试添加的视图。如果你能帮忙,那就太好了。@ElasticPanda我编辑了我的答案,其中包含了潜在的解决方案。我完全理解你所做的。我让它在shell中完美地工作,但是我不确定如何在视图中实现它。我更新了我的帖子以显示我正在尝试添加的视图。如果你能帮忙,那就太好了。@ElasticPanda我编辑了我的答案,其中包含了潜在的解决方案