【问题标题】:Django annotate according to foreign field's attributeDjango根据外部字段的属性进行注释
【发布时间】:2011-08-27 05:41:51
【问题描述】:

我通常使用类似“Tag.object.annotate(num_post=Count('post')).filter(num_post__gt=2)”的东西来获取超过 2 个帖子的标签。我想获取具有字段值(例如 post.published=True)的帖子数量并对其进行注释,以便我获得已发布帖子数量大于某个值的标签。我该怎么做?

编辑:

我想要的不是过滤带注释的对象。我想要的是这样的:Tag.objects.annotate(num_post=Count("posts that have published field set to true!"))。我想学习的是,如何将已发布字段设置为 true 的帖子在 Count 函数中。

【问题讨论】:

    标签: django model


    【解决方案1】:

    您可以将 ..._gt=2 中的 2 替换为其他变量 - 例如,传递到视图中的变量或 request.GET 值或类似的。

    这就是你想要做的吗?

    【讨论】:

      猜你喜欢
      • 2018-11-28
      • 2022-12-31
      • 1970-01-01
      • 2022-01-25
      • 2019-01-22
      • 2019-04-30
      • 2013-02-25
      • 1970-01-01
      • 2018-10-21
      相关资源
      最近更新 更多