Django:在查询中的datetime.timedelta中使用F参数 [英] Django: Using F arguments in datetime.timedelta inside a query

查看:205
本文介绍了Django:在查询中的datetime.timedelta中使用F参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果执行此操作,请使用Django模型语法:

Using Django model syntax, if I do this:

ThatModel.objects.filter(
    last_datetime__lte=now + datetime.timedelta(seconds=F("interval")))

我得到:

TypeError: unsupported type for timedelta days component: ExpressionNode

是否有一种方法可以使用纯Django语法(而不是使用Python解析所有结果)来做到这一点?

Is there a way to make this work with pure Django syntax (and not parsing all the results with Python)?

推荐答案

来自Django文档:

From django docs:

Django提供F表达式以允许进行这种比较.实例数 F()的值充当对查询中模型字段的引用.这些 然后可以在查询过滤器中使用引用来比较的值 同一模型实例上的两个不同字段.

Django provides F expressions to allow such comparisons. Instances of F() act as a reference to a model field within a query. These references can then be used in query filters to compare the values of two different fields on the same model instance.

这意味着您可以使用F()在查询中进行比较. F()返回引用,因此当将其用作timedelta对象的参数时,会出现错误ExpressionNode.您可以查看文档.您可能会检查 F()的源代码a>

That means you can use F() for comparing within queries. F() returns reference so when you use it as parameter for timedelta object, you get the error ExpressionNode. You can check the documentation. You might check the source code of F()

对于您的解决方案,您可以检查以下内容: DateModifierNode ,或者只是将interval的值保存在其他位置,然后将其作为timedelta的参数传递.

For your solution, you can check this: DateModifierNode, or just save the value of interval elsewhere and then pass it as parameter of timedelta.

这篇关于Django:在查询中的datetime.timedelta中使用F参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆