【问题标题】:Django query: Another count questionDjango 查询:另一个计数问题
【发布时间】:2011-07-29 20:21:06
【问题描述】:

您好,我似乎在 Django 中遇到了一些计数问题。我有一个显示其最新状态的项目列表。所有这些状态为“已销毁”的物品都已被移除。这打印得很好。

  status_items = models.StorageItem.objects.filter(client=client_id, itemstatushistory__isnull=False).distinct()

{% for item in status_items %}
        {{item.itemstatushistory_set.latest|cut:"Destroyed"}}
{% endfor %}

但由于某种原因我数不清。

  status_items = models.StorageItem.objects.filter(client=client_id, itemstatushistory__isnull=False).distinct().count()

TypeError while rendering: 'int' object is not iterable

【问题讨论】:

  • 顺便说一句,如果可能的话,你应该接受更多的答案。

标签: python django templates django-templates


【解决方案1】:
status_items = models.StorageItem.objects.filter(client=client_id, itemstatushistory__isnull=False).distinct().count()
{% for item in status_items %}
        {{item.itemstatushistory_set.latest|cut:"Destroyed"}}
{% endfor %}

您已将status_items 设置为整数,因此您不能再执行for item in status_items

你想达到什么目的?

【讨论】:

  • 我想要实现的目标是计算所有未销毁最新状态的项目。由于是一对多的关系,而且我只想统计每个项目的最新状态,所以有点棘手。
【解决方案2】:

之后

status_items = models.StorageItem.objects
                     .filter(client=client_id, itemstatushistory__isnull=False)
                     .distinct().count()

status_items 将包含项目数 --> intobject

{% for item in status_items %}的模板中使用它显然会产生错误。

您可以不使用count() 并在模板中访问计数,如下所示:

{{ status_items.count }}

模板系统会为你调用status_items.count()。更多信息在这里:rendering-a-context

编辑:

@Shehzad009 :我想要实现的是 计算所有最新的项目 状态没有被破坏。由于 一对多的关系,并且因为 我要统计最新状态 仅针对每个项目,这有点棘手

你可以这样定义status_items

#storageItems where itemstatushistory__status != 'Destroyed'
storage_items = models.StorageItem.objects
                .filter(client=client_id,
                        itemstatushistory__isnull=False
                        )
                .distinct()

# list of items with latest status != 'Destroyed'
status_items = [item for item in storage_items 
                if item.itemstatushistory_set.latest().description !='Destroyed']

# list of items with latest status not in ['Destroyed', 'Out']
status_items = [item for item in storage_items
                if item.itemstatushistory_set.latest().description
                   not in ['Destroyed', 'Out']]

然后在模板中:

{# show items with latest status != destroyed  #}
{% for item in status_items %}
        {{ item }}
{% endfor %}

{# items with latest status != destroyed count #}
{{ status_items|length }}

【讨论】:

  • 哦,我之前考虑过这种方法,但问题是这将计算所有具有最新状态的项目。示例:如果我说我想过滤掉所有最新状态被破坏的项目,我可以说status_items = models.StorageItem.objects.filter(client=client_id, itemstatushistory__status__description='Destroyed').distinct() ,但有一个问题。如果某个项目的状态不再被销毁,它仍将包含在列表中。
  • 看看我的编辑,我可能明白你在做什么。
  • @manji 嗯,从你定义 storage_items 的地方得到了一个non-keyword arg after keyword arg
  • 我修正了。原因是我没有遵守这个规则:Q objects 必须在查找函数中的关键字参数之前。
  • @Manji 几乎就在那里 - 首先我的程序抱怨说“StorageItem 对象没有最新的属性”。我删除了最新的。我可以看到做了什么。每个项目都有许多状态。前任。 item1 = {in,out,collected},item2 ={in,destroy},item3 ={collected,destroy,in} 等。您从代码中得到的答案会给我 {item1}。但它想要所有项目的最新状态,所以它需要的答案是 {item1, item.3}
【解决方案3】:

你使用的是哪个版本的 Django?

也许您可以考虑使用聚合和其他 QuerySet 子句中描述的注释语法:

http://docs.djangoproject.com/en/dev/topics/db/aggregation/#aggregations-and-other-queryset-clauses

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 2010-12-31
    • 2017-07-14
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多