我的提案模型定义如下:
class Proposal(models.Model):
scheduled_time = models.DateTimeField()
duration = models.IntegerField() # stores minutes as an integer
# (extra fields clipped for brevity's sake)
我想注释每个提案对象,给它一个由 schedule_time + 持续时间的 timedelta 表示计算的“结束”日期时间,例如timedelta(minutes=duration)
。但是, F() 表达式似乎不能作为timedelta()
.
>>> from datetime import timedelta
>>> from django.db.models import F
>>> from schedule.models import Proposal
>>>
>>> proposals = Proposal.objects.all()
>>> annotated = proposals.annotate(
... end=F('scheduled_time')+timedelta(minutes=F('duration')))
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: unsupported type for timedelta minutes component: F
>>>
annotate()
使用and F() 表达式可以实现这种事情吗?
编辑:注释的目的是能够过滤/排除计算的结束时间。