Как мне заставить 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()
Я хочу отфильтровать за последние 3 дня DATE_CREATED
столбец
Я использую базу данных Oracle и не могу выполнить фильтрацию по дате.