如何在python中用sqlite作为数据库为peewee列设置dedicatediff?

问题描述 投票:1回答:1

我正在创建一个项目,有3个字段,datestart,dateend,我需要用其他两个字段的减法来生成daypass字段,我看到可以通过查询的方式,但我无法使用它

sqlite peewee
1个回答
0
投票

如果你使用的是Postgres或MySQL,你应该只是减去字段。

query = MyModel.select(
    MyModel,  # All fields on MyModel.
    (MyModel.date_end - MyModel.date_start).alias('date_diff'))

如果你用的是Sqlite 那就得看你是如何存储你的日期时间了 例如,你可以使用内置的 sqlite datetime 函数来转换为 int 时间戳。

参见 https:/www.sqlite.orglang_datefunc.html 为sqlite日期时间函数列表。

© www.soinside.com 2019 - 2024. All rights reserved.