问题
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
is like this 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
回答1:
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())
Of course, you could also define a TO_UNIXTIME
stored procedure/function, but that seems a bit over the top.
来源:https://stackoverflow.com/questions/47445704/function-unix-timestamp-does-not-exist