我的提案模型定义如下:
这种操作是否可以使用
class Proposal(models.Model):
scheduled_time = models.DateTimeField()
duration = models.IntegerField() # stores minutes as an integer
# (extra fields clipped for brevity's sake)
我希望为每个提案对象添加注释,它包括一个通过scheduled_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()
和F()表达式来完成?
编辑:注释的目的是为了能够在计算出的结束时间上进行过滤/排除。
F('duration')
没有返回一个数字.. (?) - Qiau