首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django拒绝使用DB视图

Django拒绝使用DB视图
EN

Stack Overflow用户
提问于 2018-12-13 10:58:40
回答 1查看 79关注 0票数 0

我有以下情况:

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

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

  1. 对数据库的请求失败,原因如下: 错误:在字符673处不存在关系"consumption_recentconsumption“

(我在postgres级别启用了日志记录,并将完全相同的请求复制到db控制台客户端工作,没有任何修改)

  1. 对DB的请求被重试了很多次(超过30次?)。为什么会发生这种情况?是否有一个django设置来控制这一点?(我只向API发送一次请求,用curl手动发送)

编辑

这是我的模型:

代码语言:javascript
复制
class RecentConsumption(models.Model):

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

    class Meta:
        managed = False

这是SQL语句,由django生成并发送到db:

代码语言:javascript
复制
SELECT "consumption_recentconsumption"."id", "consumption_recentconsumption"."name", ... FROM "consumption_recentconsumption" LIMIT 21;

正如我前面提到的,这在django中失败,但是在直接针对db运行时工作得很好。

EDIT2

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

代码语言:javascript
复制
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

postgres在django中运行时的日志(重复30多次):

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-12-13 11:38:18

回答我自己,万一这对将来的人有帮助。

我正在PyCharm中运行PyCharm命令,它似乎对所有操作都使用事务。这意味着视图在PyCharm中的db会话中可用(因为它对所有请求都使用事务),而不是来自外部。django应用程序正在控制台中运行,没有看到视图。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53760325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档