如何让Django在下面生成/提交确切的查询:
SELECT "MESSAGE"."MSG_NO", "MESSAGE"."MSG_TYPE", "MESSAGE"."DIRECTION", "MESSAGE"."SESSION_NO", "MESSAGE"."SEQUENCE_NO", "MESSAGE"."REF_SESSION", "MESSAGE"."REF_SEQUENCE", "MESSAGE"."ACKTIME", "MESSAGE"."ACKNAKSTATUS", "MESSAGE"."PRIORITY", "MESSAGE"."DELIVMONITOR", "MESSAGE"."OBSOLESCENCE", "MESSAGE"."DISPOSITION", "MESSAGE"."TRAILER", "MESSAGE"."BYPASSED", "MESSAGE"."RESPONSE_QUEUE", "MESSAGE"."SOURCE_QUEUE", "MESSAGE"."QUEUE", "MESSAGE"."QUEUE_PRIORITY", "MESSAGE"."DATE_CREATED", "MESSAGE"."DATE_ROUTED", "MESSAGE"."INPUT_FILE", "MESSAGE"."OUTPUT_FILE", "MESSAGE"."STATUS1", "MESSAGE"."STATUS2", "MESSAGE"."STATUS3", "MESSAGE"."USERID", "MESSAGE"."TMSTAMP"
FROM "MESSAGE"
WHERE ("MESSAGE"."DATE_CREATED" >= (SYSDATE-3)
AND "MESSAGE"."DIRECTION" = 0
AND "MESSAGE"."STATUS1" = 0)
以下代码产生的查询无效:
Message.objects.using(queue_db_env).filter(STATUS1=0, DIRECTION=0, DATE_CREATED__gte=time_threshold)
下面是上面的代码产生的查询,当我手动运行它时将不起作用:
SELECT "MESSAGE"."MSG_NO", "MESSAGE"."MSG_TYPE", "MESSAGE"."DIRECTION", "MESSAGE"."SESSION_NO", "MESSAGE"."SEQUENCE_NO", "MESSAGE"."REF_SESSION", "MESSAGE"."REF_SEQUENCE", "MESSAGE"."ACKTIME", "MESSAGE"."ACKNAKSTATUS", "MESSAGE"."PRIORITY", "MESSAGE"."DELIVMONITOR", "MESSAGE"."OBSOLESCENCE", "MESSAGE"."DISPOSITION", "MESSAGE"."TRAILER", "MESSAGE"."BYPASSED", "MESSAGE"."RESPONSE_QUEUE", "MESSAGE"."SOURCE_QUEUE", "MESSAGE"."QUEUE", "MESSAGE"."QUEUE_PRIORITY", "MESSAGE"."DATE_CREATED", "MESSAGE"."DATE_ROUTED", "MESSAGE"."INPUT_FILE", "MESSAGE"."OUTPUT_FILE", "MESSAGE"."STATUS1", "MESSAGE"."STATUS2", "MESSAGE"."STATUS3", "MESSAGE"."USERID", "MESSAGE"."TMSTAMP"
FROM "MESSAGE"
WHERE ("MESSAGE"."DATE_CREATED" >= 2018-09-15 12:47:43.784709
AND "MESSAGE"."DIRECTION" = 0
AND "MESSAGE"."STATUS1" = 0)
这是由于"MESSAGE"."DATE_CREATED" >= 2018-09-15 12:47:43.784709
造成的,查询仅在我将DATE_CREATE
换为"MESSAGE"."DATE_CREATED" >= (SYSDATE-3)
子句时才有效
我的views.py具有以下代码:
class MessageList(APIView):
def get(self, request, queue_db_env, queue_name, queue_id, format=None):
#
# time_threshold = datetime.now() - timedelta(hours=36)
now = timezone.now()
time_threshold = now - datetime.timedelta(hours=36)
print(time_threshold.timestamp())
str_time_threshold = time_threshold.timestamp()
#
messages = Message.objects.using(queue_db_env).filter(STATUS1=0, DIRECTION=0, DATE_CREATED__gte=time_threshold)
print(messages.query)
serializer = MessageSerializer(messages, many=True)
return Response({"Queue": queue_name, "Queue_ID": queue_id, "Queue_Messages": serializer.data})
我的models.py是:
from django.db import models
class Message(models.Model):
MSG_NO = models.IntegerField(primary_key=True,)
MSG_TYPE = models.IntegerField()
DIRECTION = models.IntegerField()
SESSION_NO = models.IntegerField()
SEQUENCE_NO = models.IntegerField()
REF_SESSION = models.IntegerField()
REF_SEQUENCE = models.IntegerField()
ACKTIME = models.DateTimeField(max_length = 7)
ACKNAKSTATUS = models.IntegerField()
PRIORITY = models.CharField(max_length = 1)
DELIVMONITOR = models.IntegerField()
OBSOLESCENCE = models.IntegerField()
DISPOSITION = models.IntegerField()
TRAILER = models.IntegerField()
BYPASSED = models.IntegerField()
RESPONSE_QUEUE = models.IntegerField()
SOURCE_QUEUE = models.IntegerField()
QUEUE = models.IntegerField()
QUEUE_PRIORITY = models.IntegerField()
DATE_CREATED = models.DateTimeField()
DATE_ROUTED = models.DateTimeField()
INPUT_FILE = models.IntegerField()
OUTPUT_FILE = models.IntegerField()
STATUS1 = models.IntegerField()
STATUS2 = models.IntegerField()
STATUS3 = models.IntegerField()
USERID = models.CharField(max_length = 8)
TMSTAMP = models.DateTimeField()
我想根据过去DATE_CREATED
的筛选条件过滤过去3天
我正在使用Oracle数据库,但无法按日期进行过滤。
答案 0 :(得分:2)
time_threshold = now - datetime.timedelta(hours=36)
一天有24个小时。因此36小时= 1.5天。如果要在最近三天进行过滤,则需要
timedelta(hours=72)