我需要過濾比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