更新
@datetime_as_timestamp.expression
def datetime_as_timestamp(cls):
return db.func.timestamp(cls.date, cls.time)
到
@datetime_as_timestamp.expression
def datetime_as_timestamp(cls):
return cls.date + cls.time
原始
我的模特中有日期和时间。在另一个表中有他们的时区,但为简单起见,我将手动完成。我想按日期+时间+时区进行过滤。
class Foo(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date)
time = db.Column(db.Time)
@hybrid_property
def datetime_as_timestamp(self):
return time.mktime(
datetime.datetime.combine(self.date, self.time).timetuple()
)
@datetime_as_timestamp.expression
def datetime_as_timestamp(cls):
return db.func.timestamp(cls.date, cls.time)
但是当我执行过滤查询时
In [1]: Foo.query.filter(func.timezone('Australia/Brisbane', Foo.datetime_as_timestamp) <= func.timezone('UTC', func.current_timestamp())).one()
我收到错误
psycopg2.ProgrammingError) syntax error at or near "foo"
LINE 3: WHERE timezone('Australia/Brisbane', timestamp(foo.da...
^
[SQL: 'SELECT foo.id AS checkin_flight_id, foo.date AS checkin_flight_date, foo.time AS checkin_flight_time \nFROM foo \nWHERE timezone(%(timezone_1)s, timestamp(foo.date, foo.time)) <= timezone(%(timezone_2)s, CURRENT_TIMESTAMP)'] [parameters: {'timezone_2': 'UTC', 'timezone_1': 'Australia/Brisbane'}]
我错过了什么,我的错误在哪里?是否有可能以另一种方式解决我的问题?
答案 0 :(得分:0)
我发现这个Q&A非常有帮助。我改变了
@datetime_as_timestamp.expression
def datetime_as_timestamp(cls):
return db.func.timestamp(cls.date, cls.time)
到
@datetime_as_timestamp.expression
def datetime_as_timestamp(cls):
return cls.date + cls.time
一切正常