Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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
Sql Django按日期检索结果操作查询_Sql_Django - Fatal编程技术网

Sql Django按日期检索结果操作查询

Sql Django按日期检索结果操作查询,sql,django,Sql,Django,我有一个模型来制作这样的目录 类Productmodels。模型: 项目=型号。CharFieldmax_长度=10 描述=型号。CharFieldmax_长度=140 单位=型号。CharFieldmax_长度=5 有效期=型号。IntegerFielddefault=入院后365天 过期\警告=型号。IntegerFielddefault=过期前30天 ... 我的库存如下: 类Inventorymodels。模型: product=型号。ForeignKeyProduct 数量=型号。De

我有一个模型来制作这样的目录

类Productmodels。模型: 项目=型号。CharFieldmax_长度=10 描述=型号。CharFieldmax_长度=140 单位=型号。CharFieldmax_长度=5 有效期=型号。IntegerFielddefault=入院后365天 过期\警告=型号。IntegerFielddefault=过期前30天 ... 我的库存如下:

类Inventorymodels。模型: product=型号。ForeignKeyProduct 数量=型号。DecimalFieldedFault=0,小数位数=2,最大位数=10 入院=模型。DateTimeFieldDauto\u now\u add=True ... 现在,我想检索其到期日期即将到来或已经到达的所有库存对象。 对于原始SQL查询,它将类似于:

选择产品、数量、准入、到期、到期警告 在I.Product=P.id上以P的形式加入产品时从库存
其中DATEADDday,expiration\u warning*-1,DATEADDday,expiration,administration也许类似的东西可以满足您的需要:

from datetime import datetime, timedelta

expiring_inventory = []
expired_inventory = []
for inventory in Inventory.objects.select_related('product').all(): # 1 query
    p = inventory.product

    expire_datetime = inventory.admission + timedelta(days=p.expiration)
    expiring_datetime = expire_datetime - timedelta(days=p.expiration_warning)

    if expiring_datetime <= datetime.now() < expire_datetime: # not quite expired
        expiring_inventory.append(inventory)
    if expire_datetime <= datetime.now(): # expired
        expired_inventory.append(inventory)

您可以在django文档中阅读更多信息。

我认为这应该可以做到,但它是用python完成整个操作的,目标是使数据库使用django查询语法,因为它更快,知道吗?非常感谢。