Django QuerySet滤波器使用模型属性查询查询

发布于 2025-02-02 19:52:38 字数 524 浏览 1 评论 0原文

假设我有一个模型示例

class Example
   start_time = models.DateTimeField(db_index=True)
   duration = models.IntegerField(default=0)

和一个QuerySet类

class ExampleQuerySet
    def live_objects():
        self.active().filter(start_time__gte=get_minutes_before(get_utc_now(),5), start_time__lte=get_minutes_after(get_utc_now(), self.duration))

。开始时间和当前时间<启动时间 +对象的持续时间

,但是上面的查询失败了,self Object没有持续时间属性

我如何编写查询,我可以在其中滤除当前现场的对象?

Let's say I have a model Example

class Example
   start_time = models.DateTimeField(db_index=True)
   duration = models.IntegerField(default=0)

And a QuerySet class

class ExampleQuerySet
    def live_objects():
        self.active().filter(start_time__gte=get_minutes_before(get_utc_now(),5), start_time__lte=get_minutes_after(get_utc_now(), self.duration))

The objective of the live_objects() method is to return those objects which have their current time > start time and current time < start time + duration of the object

But this above query is failing with the error that self object doesn't have duration attribute

How do I go about writing a query where I can filter out objects that are currently live ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文