Django拒绝使用数据库视图

时间:2018-12-13 10:58:40

标签: django postgresql django-rest-framework

我有以下情况:

  • 我已经在数据库中定义了正确的视图,请注意该视图是根据Django约定命名的
  • 我确保我的模型不受django管理。因此,使用managed=False
  • 定义了创建的迁移
  • 数据库视图本身运行正常。

触发API端点时,会发生两件奇怪的事情:

  1. 对数据库的请求失败,并显示:

    错误:在字符673上不存在关系“ consumption_recentconsumption”

(我已在postgres级别启用日志记录,并且将完全相同的请求复制粘贴到数据库控制台客户端中可以正常工作,无需任何修改

  1. 向数据库的请求被重试很多次(超过30次?)。为什么会这样呢?是否有django设置可以控制此设置? (我仅使用curl手动将请求发送到API一次)

编辑

这是我的模特

class RecentConsumption(models.Model):

    name = models.CharField(max_length=100)
    ...

    class Meta:
        managed = False

这是SQL生成的django语句,并发送到数据库:

SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", ... FROM "consumption_recentconsumption" LIMIT 21;

正如我提到的那样,此操作无法通过django,但是在直接针对数据库运行时效果很好。

EDIT2

直接运行sql时,来自postgres的日志:

2018-12-13 11:12:02.954 UTC [66] LOG:  execute <unnamed>: SAVEPOINT JDBC_SAVEPOINT_4
2018-12-13 11:12:02.955 UTC [66] LOG:  execute <unnamed>: SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", "consumption_recentconsumption"."date", "consumption_recentconsumption"."psc", "consumption_recentconsumption"."material", "consumption_recentconsumption"."system", "consumption_recentconsumption"."env", "consumption_recentconsumption"."objs", "consumption_recentconsumption"."size", "consumption_recentconsumption"."used", "consumption_recentconsumption"."location", "consumption_recentconsumption"."WWN", "consumption_recentconsumption"."hosts", "consumption_recentconsumption"."pool_name", "consumption_recentconsumption"."storage_name", "consumption_recentconsumption"."server" FROM "consumption_recentconsumption" LIMIT 21
2018-12-13 11:12:10.038 UTC [66] LOG:  execute <unnamed>: RELEASE SAVEPOINT JDBC_SAVEPOINT_4

运行django时来自postgres的日志(重复30次以上):

2018-12-13 11:13:50.782 UTC [75] LOG:  statement: SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", "consumption_recentconsumption"."date", "consumption_recentconsumption"."psc", "consumption_recentconsumption"."material", "consumption_recentconsumption"."system", "consumption_recentconsumption"."env", "consumption_recentconsumption"."objs", "consumption_recentconsumption"."size", "consumption_recentconsumption"."used", "consumption_recentconsumption"."location", "consumption_recentconsumption"."WWN", "consumption_recentconsumption"."hosts", "consumption_recentconsumption"."pool_name", "consumption_recentconsumption"."storage_name", "consumption_recentconsumption"."server" FROM "consumption_recentconsumption" LIMIT 21
2018-12-13 11:13:50.783 UTC [75] ERROR:  relation "consumption_recentconsumption" does not exist at character 673
2018-12-13 11:13:50.783 UTC [75] STATEMENT:  SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", "consumption_recentconsumption"."date", "consumption_recentconsumption"."psc", "consumption_recentconsumption"."material", "consumption_recentconsumption"."system", "consumption_recentconsumption"."env", "consumption_recentconsumption"."objs", "consumption_recentconsumption"."size", "consumption_recentconsumption"."used", "consumption_recentconsumption"."location", "consumption_recentconsumption"."WWN", "consumption_recentconsumption"."hosts", "consumption_recentconsumption"."pool_name", "consumption_recentconsumption"."storage_name", "consumption_recentconsumption"."server" FROM "consumption_recentconsumption" LIMIT 21

1 个答案:

答案 0 :(得分:0)

回答自己,以防将来对某人有帮助。

我正在CREATE VIEW中运行PyCharm命令,该命令似乎对所有操作都使用事务。这意味着该视图在PyCharm的db会话中可用(因为该视图将事务用于所有请求),但不能在外部使用。 django应用程序正在控制台中运行,并且看不到该视图。

解决方案只是在PyCharm中提交事务,以使其完全可见。

最终的解决方案是通过django迁移创建视图。