【问题标题】:function UNIX_TIMESTAMP does not exist函数 UNIX_TIMESTAMP 不存在
【发布时间】:2018-05-06 20:11:38
【问题描述】:

我正在尝试将我的日期时间转换为 unix 时间戳。我尝试了几种方法,错误都是一样的。我不太确定我应该做什么。

date_joined是这样的2017-09-30 10:24:44.954981+00:00

function unix_timestamp(timestamp with time zone) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.


User.objects.annotate(photo_time=Func(F('date_joined'),function='UNIX_TIMESTAMP'))
User.objects.extra(select={'photo_time':"to_unixtime(date_joined)"})
#also tried and UNIX_TIMESTAMP

【问题讨论】:

    标签: django postgresql django-models


    【解决方案1】:

    那是因为 Postgres 不允许你这样做 (see here)。如果你真的不需要UNIX_TIMESTAMP,你需要Extract

    User.objects.annotate(photo_time=Extract('date_joined', 'epoch').get())
    

    当然,您也可以定义一个TO_UNIXTIME 存储过程/函数,但这似乎有点过头了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 1970-01-01
      相关资源
      最近更新 更多