根据当前时间和上次更新时间之间的小时计算时间差



我想识别已经更新了24小时的对象。

我的型号

class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
image = models.ImageField(upload_to='products/%Y/%m/%d/')
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)  

我的观点:

def home(request):
products = Product.objects.filter(available=True)
return render(request, 'shop_app/home.html', {'products': products})

我猜您试图得到一个结果,其中CurrentDateTime - 24 hours字段的值为1 day,即updated >= valid_dt

这可以通过在过滤器标签中添加另一个参数来实现,即valid_dt,其中timedelta是使用relativedelta计算的有效日期时间(当前日期时间-1天(

PD_4注意:如果您想更改时间,而不是以天/月/年为单位,您可以使用CCD_8

最新更新