0
我用的Postgres我的Django项目,并为复杂的查询到DB I使用connection.cursor()。今天我与日期时间原始SQL查询的问题过滤器:Django的原始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没有收到这个结果?
绕过Django的ORM可能会非常棘手。在许多情况下,最好坚持构建'QuerySet';上面显示的查询可以很容易地用这种方式执行。 – Chris
无论如何,[与PostgreSQL的两个连接可能会看到不同的数据](https://www.postgresql.org/docs/current/static/mvcc-intro.html)。这是[一个功能](https://en.wikipedia.org/wiki/Multiversion_concurrency_control)。您是否使用手动查询进行交易?没有更多的信息,很难知道这是否是这里发生的事情。 – Chris