django:datediff sql 查询?

发布于 2024-09-26 21:09:47 字数 280 浏览 0 评论 0原文

我正在尝试在 Django 中执行与以下 SQL 相同的操作:

SELECT * FROM applicant WHERE date_out - date_in >= 1 AND date_out - date_in <= 6

我可以将其作为 RAW sql 查询来执行,但是在处理 RawQuerySet 而不是常规 QuerySet 对象时,这变得令人沮丧,因为我希望能够过滤稍后在代码中。

I'm trying to do the equivalent of the following SQL in Django:

SELECT * FROM applicant WHERE date_out - date_in >= 1 AND date_out - date_in <= 6

I can do this as a RAW sql query, but this is becoming frustrating in dealing with a RawQuerySet instead of a regular QuerySet object as I would like to be able to filter it later in the code.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹梦 2024-10-03 21:09:47

我遇到了 Django 本身不支持 Datediff (以及其他数据库等效项)的问题,并且需要针对特定​​项目多次使用这样的函数。

经过进一步阅读,很明显,计算两个日期间隔的实现在主要数据库风格之间存在很大差异。这可能就是 Django 中还没有原生抽象函数的原因。所以我为 datediff 编写了自己的 Django ORM 函数:

请参阅:mike-db-tools Github 存储库

您将看到在各个数据库的文档字符串中编写的数据库后端之间不同的语法。 Datediff 支持 sqlite、MySQL / MariaDB、PostgreSQL 和 Oracle。

用法(Django 1.8+):

from db_tools import Datediff

# Define a new dynamic fields to contain the calculated date difference
applicants = Applicant.objects.annotate(
    days_range=Datediff('date_out','date_in', interval='days'),
)

# Now you can use this dynamic field in your standard filter query
applicants = applicants.filter(days_range__gte=1, days_range__lte=6)

当涉及到我的代码时,我真的很傻,所以我鼓励你分叉和改进。

I came across the issue of Django not natively supporting Datediff (and other database equivalents), and needed to use such a function many times for a particular project.

Upon further reading, it became clear that the implementation of calculating an interval from two dates differs widely between major database flavours. This is probably why it's not got a native abstraction function in Django yet. So I wrote my own Django ORM function for datediff:

See: mike-db-tools Github repository

You'll see the varying syntax between the database backends written in the docstrings for the respective databases. Datediff supports sqlite, MySQL / MariaDB, PostgreSQL and Oracle.

Usage (Django 1.8+):

from db_tools import Datediff

# Define a new dynamic fields to contain the calculated date difference
applicants = Applicant.objects.annotate(
    days_range=Datediff('date_out','date_in', interval='days'),
)

# Now you can use this dynamic field in your standard filter query
applicants = applicants.filter(days_range__gte=1, days_range__lte=6)

I'm really quite derpy when it comes to my code, so I encourage you to fork and improve.

怪我入戏太深 2024-10-03 21:09:47

您可以使用 extra() 方法并传入 where 关键字参数。 where 的值应该是一个包含上述查询的 SQL WHERE 子句的列表。我使用 Postgresql 8.4 对此进行了测试,这就是我的情况:

q = Applicant.objects.extra(where = ["""date_part('day', age(date_out, date_in)) >= 1 and
      date_part('day', age(date_out, date_in)) <= 6"""])

这将返回一个有效的 QuerySet 实例。

You can use the extra() method and pass in a where keyword argument. The value of where should be a list that contains the SQL WHERE clause of the query above. I tested this with Postgresql 8.4 and this is what it looked like in my case:

q = Applicant.objects.extra(where = ["""date_part('day', age(date_out, date_in)) >= 1 and
      date_part('day', age(date_out, date_in)) <= 6"""])

This will return you a valid QuerySet instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文