【问题标题】:Datetime differences in Django using SQLite使用 SQLite 的 Django 中的日期时间差异
【发布时间】:2015-12-16 13:27:27
【问题描述】:

我正在创建一个需要执行日期差异的 Django 应用程序。给定一个在 Postgres 上具有 start_dateend_date 的模型,两者都是 DateFields,它的工作原理如下:

model.objects.annotate(difference=F(end_date)-F(start_date))

可以正常工作。但是,在 SQLite 后端,这也不能正常工作。

它返回一个字符串,而不是给出一个 timedelta(或类似的),这大约是年份的差异。但是,SQLite 有一个命令 julianday() 将日期转换为“儒略日”,这至少可以用来计算天数。

例如,在dbshell 上,这将给出正确的天数差异:

SELECT julianday(end_date) - julianday(start_date) FROM 'appname'.'model'; 

有没有办法:

  1. 检查数据库后端 - 例如。是不是 SQLite?

  2. 然后,如果是 SQLite,则将其包装在 julianday 函数中?

【问题讨论】:

标签: python django postgresql sqlite


【解决方案1】:

我实际上是通过创建一个只在 SQLite 上执行任何操作的自定义数据库函数来解决这个问题的,如下所示:

from django.db.models.expressions import Func
# SQLite function to force a date time subtraction to come out correctly.
# This just returns the expression on every other database backend.
class ForceDate(Func):
    function = ''
    template = "%(expressions)s"
    def __init__(self, expression, **extra):
        self.__expression = expression
        super(ForceDate, self).__init__(expression, **extra)

    def as_sqlite(self, compiler, connection):
        self.function = 'julianday'
        self.template = 'coalesce(%(function)s(%(expressions)s),julianday())*24*60*60*1000*1000' # Convert julian day to microseconds as used by Django DurationField
        return super(ForceDate, self).as_sql(compiler, connection)

然后,在代码中使用 ExpressionWrapper 将差异强制转换为 DurationField(请注意,这只适用于 Django 1.8)

ExpressionWrapper(db.ForceDate(F(a))-db.ForceDate(F(b)), output_field=DurationField())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    相关资源
    最近更新 更多