Django 查询:另一个计数问题

发布于 2024-10-30 06:01:12 字数 550 浏览 1 评论 0原文

你好,我似乎在 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()

渲染时出现类型错误:“int”对象不可迭代

Hello I seem to be having some problem involving counting in Django. I have a list of items which only displays its latest status. All of these items that has a status "Destroyed" have been removed. This prints nicely.

  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 %}

But I can't count for some reason.

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

TypeError while rendering: 'int' object is not iterable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆啦不做梦 2024-11-06 06:01:12

status_items之后

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

将包含项目计数 --> 在 {% for item in status_items %} 模板中使用int 对象

显然会产生错误。

您可以不使用 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 }}

after

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

status_items will contain the count of items --> an intobject

using it in the template in {% for item in status_items %} will obviously generate an error.

you can leave it without the count() and in the template access to the count like this:

{{ status_items.count }}

The template system will call status_items.count() for you. More infos here: rendering-a-context

EDIT:

@Shehzad009 : What I am trying to achieve, is to
count all items that has their latest
status not destroyed. Because of the
one to many relationship, and because
I want to count the latest statuses
for each item only, it is a bit tricky

you could define status_items like this:

#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']]

then in the template:

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

{# items with latest status != destroyed count #}
{{ status_items|length }}
Hello爱情风 2024-11-06 06:01:12
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 设置为整数,因此您无法对 status_items 中的项目执行 操作。

你想达到什么目的?

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 %}

You've set status_items to an integer, so you can't then do for item in status_items.

What are you trying to achieve?

七度光 2024-11-06 06:01:12

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

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

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

Which version of Django are you on?

Maybe you could consider using the annotate syntax like described here under Aggregations and other QuerySet clauses:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文