我在我的Django项目中使用PostgreSQL,对于复杂的数据库查询,我使用connection.cursor()。今天,我在使用带有datetime筛选条件的原生SQL查询时遇到了问题:
with connection.cursor() as cursor:
cursor.execute(
"SELECT * from orders_orderstatus WHERE datetime > '2017-09-05 16:07:16'"
)
row = [item[0] for item in cursor.fetchall()]
return row
结果是空列表。但如果我从psql控制台查询,我会看到结果不为空:
SELECT * FROM orders_orderstatus WHERE datetime > '2017-09-05 16:07:16';
id | status | datetime
----+--------------------+-------------------------------+
256 | created | 2017-09-05 16:10:59.602091+07
257 | delivered | 2017-09-05 16:11:00.834547+07
258 | created | 2017-09-05 16:11:03.499364+07
为什么 Django 没有收到这些结果?
QuerySet
;上面显示的查询可以轻松地使用这种方式执行。 - Chris