【问题标题】:Calculation sum of two fields to filter entries计算两个字段的总和以过滤条目
【发布时间】:2018-05-05 03:22:42
【问题描述】:

我有一个名为 Foo 的模型类,具有以下字段 startduration

class Foo(models.Model):
    start = models.DateTimeField()
    duration = models.DurationField()

我想通过检查 start 加上 duration 是否小于某个时间来过滤 Foo 表中的所有条目。

【问题讨论】:

标签: python mysql sql django orm


【解决方案1】:

你应该可以使用 F 表达式

from django.db.models import F
Foo.objects.filter(duration__lt=yourTime - F('start'))

您可能需要使用timedelta。查看更多示例here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多