Django查询:想要计算空值

发布于 2024-10-30 17:20:44 字数 882 浏览 1 评论 0原文

我似乎在数数时遇到了问题。我有一个列表项,其中每个项目可以存储许多状态,尽管我只对其最新状态感兴趣。现在看看我的看法。

storage_items = StorageItem.objects\
                .filter(client=client_id,itemstatushistory__isnull=False)\
                .distinct()

total_items_in_stock = [item for item in storage_items \
                        if item.itemstatushistory_set.latest().status.description \
                        not in ['Destroyed','Permanent Retrieval']]

total_items_in_stock 显示所有不具有名为已销毁永久检索 最新状态的项目。然而,这有一个问题。

假设我的数据库中有一些项目 - 例如 item1 {in、out、destroyed}、item2 = {destroyed、in}、item3 = {permanent检索}、item4 = {}。因为它寻找最新状态,所以它会打印 {item2}。我现在想在 staock 的总项目中打印 item4 。基本上 item4 是一个没有状态的项目。但由于它尚未被销毁永久检索,因此需要将其包含在列表中。但我似乎无法找到解决办法。我希望我已经把一切说清楚了。

模板

{{total_items_in_stock|length}}

I seem to have a problem trying to count. I have a list item where each item can store many statuses, although I am only interested in it's latest status only. Now look at my views.

storage_items = StorageItem.objects\
                .filter(client=client_id,itemstatushistory__isnull=False)\
                .distinct()

total_items_in_stock = [item for item in storage_items \
                        if item.itemstatushistory_set.latest().status.description \
                        not in ['Destroyed','Permanent Retrieval']]

total_items_in_stock shows all items which do not have a latest status called Destroyed and Permanent Retrieval. There is a problem with this however.

Suppose I have some items in my database - say item1 {in, out, destroyed}, item2 = {destroyed, in}, item3 = {permanent retrieve}, item4 = {}. Because it looks for the latest status, it will print {item2}. I now want to print item4 as well in the total items in staock. Basically item4 is an item without a status. But since it has not been Destroyed or Permanent Retrieval it needs to be included in the list. But I can't seem to find a way out of this. I hope I have made everything clear.

Template

{{total_items_in_stock|length}}

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

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

发布评论

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

评论(2

骷髅 2024-11-06 17:20:49

作为替代方案,您可以将 in_stock 方法添加到您的 StorageItem 类中。

沿着这些思路:

def in_stock(self):
    if 'destroyed' and 'permanent_retrieval' not in self.itemstatushistory_set.latest().status.description:
        return True 

然后你可以简化你的列表理解:

total_items_in_stock = [item for item in storage_items if item.in_stock()]

As an alternative you could add an in_stock-method to your StorageItem class.

Something along these lines:

def in_stock(self):
    if 'destroyed' and 'permanent_retrieval' not in self.itemstatushistory_set.latest().status.description:
        return True 

Then you could simplify your list comprehension:

total_items_in_stock = [item for item in storage_items if item.in_stock()]
我还不会笑 2024-11-06 17:20:49

试试这个:

storage_items = models.StorageItem.objects.filter(client=client_id).distinct()

total_items_in_stock = [item for item in storage_items
        if not item.itemstatushistory_set.exists() or 
           item.itemstatushistory_set.latest().status.description not in ['Destroyed','Permanent Retrieval']]

Try this:

storage_items = models.StorageItem.objects.filter(client=client_id).distinct()

total_items_in_stock = [item for item in storage_items
        if not item.itemstatushistory_set.exists() or 
           item.itemstatushistory_set.latest().status.description not in ['Destroyed','Permanent Retrieval']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文