函数UNIX_TIMESTAMP不存在 [英] function UNIX_TIMESTAMP does not exist

查看:41
本文介绍了函数UNIX_TIMESTAMP不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期时间转换为Unix时间戳.我已经尝试过几种方法,但错误都是一样的.我不太确定该做些什么.

I am trying to convert my datetime to unix timestamp. I've tried doing several ways and the error is all the same. I am not very sure what I am suppose to do.

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

推荐答案

这是因为Postgres不允许您这样做(

That's because Postgres won't let you do that (see here). If you really don't need the UNIX_TIMESTAMP, you need Extract

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

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

Of course, you could also define a TO_UNIXTIME stored procedure/function, but that seems a bit over the top.

这篇关于函数UNIX_TIMESTAMP不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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