I am trying to filter a Django queryset by timestamp (less than a certain value). However, the filter seems to be letting through records that are NOT less than the specified timestamp. Here is an example function:
def check_jobs_status_3():
current_time = datetime.utcnow()
time_threshold = current_time - timedelta(seconds=60)
print("$$$$$$$$$$$$ current_time = {}, timedelta = {}, time_threshold = {}".format(current_time,timedelta(seconds=60),time_threshold))
stuck_jobs_qs = Job.objects.filter(last_updated__lt=time_threshold)
for stuck_job in stuck_jobs_qs:
print("############################## Job #{} (last_updated = {}) no encoding status after {} seconds. Re-enqueueing.".format(stuck_job.id,stuck_job.last_updated,get_video_encoding_timeout_seconds()))
Here is the output:
$$$$$$$$$$$$ current_time = 2019-03-14 20:54:15.221554, timedelta = 0:01:00, time_threshold = 2019-03-14 20:53:15.221554
############################## Job #20 (last_updated = 2019-03-14 20:54:15.221264+00:00) no encoding status after 60 seconds. Re-enqueueing.
As you can see, I am receiving a record with last_updated set to 2019-03-14 20:54:15, which is NOT less than the filter value of 2019-03-14 20:53:15
Here is the definition of the last_updated field:
last_updated = models.DateTimeField(auto_now=True)
What could the problem be?
Use Django's timezone-aware method django.utils.timezone.now
.
from django.utils import timezone
# ...
def check_jobs_status_3():
current_time = timezone.now() # change this
time_threshold = current_time - timedelta(seconds=60)
print("$$$$$$$$$$$$ current_time = {}, timedelta = {}, time_threshold = {}".format(current_time,timedelta(seconds=60),time_threshold))
stuck_jobs_qs = Job.objects.filter(last_updated__lt=time_threshold)
for stuck_job in stuck_jobs_qs:
print("############################## Job #{} (last_updated = {}) no encoding status after {} seconds. Re-enqueueing.".format(stuck_job.id,stuck_job.last_updated,get_video_encoding_timeout_seconds()))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.