from django.db.models import Sum from goods_info.models import GoodsInventoryModel def get_inventory_by_goods(goods_id, warehouse_id=0): """ 1、可以返回指定货品的总库存 2、可以返回知道货品、指定仓库的库存 :param goods_id: 指定的货品ID :param warehouse_id: 指定的仓库ID,如果为0则查询所有仓库 :return: """ sum_inventory = 0 if warehouse_id == 0: # 查询总库存 result = GoodsInventoryModel.objects.filter(goods_id=goods_id).aggregate(sum=Sum('cur_inventory')) else: result = GoodsInventoryModel.objects.filter(goods_id=goods_id, warehouse_id=warehouse_id).aggregate(sum=Sum('cur_inventory')) if result: sum_inventory = result['sum'] return sum_inventory