Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django查询:要计算空值吗_Python_Django_Views_Count - Fatal编程技术网

Python Django查询:要计算空值吗

Python Django查询:要计算空值吗,python,django,views,count,Python,Django,Views,Count,我似乎在数数的时候有点问题。我有一个列表项,其中每个项可以存储许多状态,尽管我只对它的最新状态感兴趣。现在看看我的观点 storage_items = StorageItem.objects\ .filter(client=client_id,itemstatushistory__isnull=False)\ .distinct() total_items_in_stock = [item for item in storage_

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

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\u items\u in\u stock
显示所有没有最新状态的项目,这些状态称为
已销毁
永久检索
。然而,这有一个问题

假设我的数据库中有一些项,比如item1{in,out,destromed},item2={destromed,in},item3={permanent retrieve},item4={}。因为它查找最新状态,所以将打印{item2}。我现在想在staock中的所有项目中打印项目4。基本上,项目4是一个没有状态的项目。但是,由于它没有被
销毁
永久
检索,因此需要将其包括在列表中。但我似乎找不到一条出路。我希望我已经把一切都说清楚了

模板

{{total_items_in_stock|length}}
试试这个:

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

作为替代方法,您可以将
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()]

由于某种原因,您在库存中定义的
total\u items\u部分似乎得到了无效语法