[英]How to get the number of days by subtraction from the datetime field and then filter in Django?
这是模型:
class Work(models.Model):
start_date = DateTimeField()
end_date = DateTimeField()
sot = IntegerField(help_text='days')
我想从结束时间中减去开始时间以获得与 sot 比较的天数并过滤掉我想要的数据。
我可以通过循环得到我想要的东西,但这很不雅,有没有办法通过ORM来做到这一点?
[w for w in Work.objects.all() if (w.end_date - w.start_date).days < w.sot ]
您可以使用F expiression
F
来引用字段end_date
和start_date
end_date
减去start_date
,这给出了以微秒为单位的时间戳差异。sot
字段过滤日差Work.objects.filter(sot__gt=(F('end_date')-F('start_date'))/(3600*24*1000*1000))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.