我需要过滤比X
更早的天数。我意识到这个问题存在的位置:django filter older than day(s)?Django - 过滤超过X天的对象
但是,我并不想做到这些,因为天数,在我的情况下,生活在模型内部:
class Post(models.Model):
title = models.CharField(max_length=200)
description = models.CharField(max_length=500)
createdAt = models.DateTimeField(default=datetime.now, blank=True)
plan = models.ForeignKey(Plan) # This model has the number of days
这是查询我有这么远:
编辑:我改变了days.plan一部分the_post.plan.days
意思是,我使用比较的天数在每个岗位的场。
Post.objects.filter(createdAt__lte=datetime.now() - timedelta(days=the_post.plan.days))
请注意plan.days
部分查询。如何为此查询参考the_post.plan.days
?可能吗?
嗨@lucasnadalutti这似乎是正确的答案,但是,我得到这个错误:'ValueError:不天真的日期时间(tzinfo已设置)'不知道为什么 – danielrvt
尝试用'时区'替换'datetime.now()' .now()',它是从'django.utils import timezone'输入的 – lucasnadalutti