gpt4 book ai didi

python - django 中未定义 DateTimeField 错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:14 25 4
gpt4 key购买 nike

我想删除年龄超过一天的模特的视频。

模型.py

from __future__ import unicode_literals
import urllib
from django.core.files import File
from django.db import models
from PIL import Image
from datetime import datetime
from django.utils import timezone
import os
class video(models.Model):
video_title = models.CharField(max_length=250)
video_link = models.CharField(max_length=250)
video_thumbnail = models.ImageField(upload_to = '',blank = True)
image_url = models.CharField(max_length=250,blank = True)
upload_date = models.DateTimeField(blank=True)
def cache(self):
if self.image_url and not self.video_thumbnail:
result = urllib.urlretrieve(self.image_url)
self.video_thumbnail.save(
os.path.basename(self.image_url),
File(open(result[0]))
)
self.save()

def __str__(self):
return self.video_title + "," + self.video_link + "," + str(self.upload_date)

在 shell 中,我执行查询来删除时间超过一天的视频,如下所示:

video.objects.filter((datetime.datetime.now() - (upload_date)).total_hours > 24)

但我收到以下错误:

In [6]: video.objects.filter((datetime.datetime.now() - (upload_date)).total_hours > 24)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-0c1ffc162513> in <module>()
----> 1 video.objects.filter((datetime.datetime.now() - (upload_date)).total_hours > 24)

NameError: name 'upload_date' is not defined

为什么我收到错误?

最佳答案

该查询是倒退的。您不能将 datetime.datetime.now() 作为第一个参数传递;这是为了比较字段名称。您需要反转查询,以便查询 upload_date 在现在负 24 小时之前的所有视频:

target_time = datetime.datetime.now() - datetime.timedelta(hours=24)
video.objects.filter(upload_date__lte=target_time)

关于python - django 中未定义 DateTimeField 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41185851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com