Django QuerySet滤波器使用模型属性查询查询
假设我有一个模型示例
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论