Django 1.8 inspectdb命令根据文档看不到PostgreSQL视图 [英] Django 1.8 inspectdb command doesn't see PostgreSQL views as per documentation

查看:74
本文介绍了Django 1.8 inspectdb命令根据文档看不到PostgreSQL视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有PostgreSQL数据库的Django 1.8应用程序.我从命令行运行django inspectdb来检查视图的模型,但是视图未显示在模型输出中.

I have a Django 1.8 application with a PostgreSQL database. I run the django inspectdb from the command line to examine models for the views, but the views don't show up in the model output.

以下是版本输出:

17:36 $ python well/manage.py --version
1.8.2

这是psql看到的内容:

And here's what psql sees:

\dv
                List of relations
 Schema |             Name              | Type |  Owner  
--------+-------------------------------+------+---------
 public | hospitalizations_over_30_days | view | dwatson
 public | interval_30_days              | view | dwatson
(2 rows)

摘自django 1.8.2文档:

From the django 1.8.2 documentation:

New in Django 1.8:
A feature to inspect database views was added. In previous versions, only tables (not views) were inspected.

如何使PostgreSQL视图显示在Django 1.8.2 inspectdb输出中?

How can I get the PostgreSQL views to appear in the Django 1.8.2 inspectdb output?

推荐答案

从Django 1.10开始,您可以简单地将单个视图命名为 inspectdb 命令的参数:

As of Django 1.10, you can simply name an individual view as a parameter to your inspectdb command:

python well/manage.py inspectdb hospitalizations_over_30_days

默认的 inspectdb 仅会输出表的models.py,但是视图的模型可以通过命名来单独生成.

The default inspectdb will only output models.py for tables, but models for views can be generated individually by naming them.

在Django 2.1及更高版本中,如果要 inspectdb 为所有表视图生成模型,请

In Django 2.1 and above, if you want inspectdb to generate models for all tables and views, use the inspectdb --include-views option, which I contributed to Django 2.1 as a result of this question!

python well/manage.py inspectdb --include-views

要在Django 2.0及更低版本中为表和视图生成模型,必须编辑Django源代码.在Django 2.0中,更改第57行 django/core/management/commands/inspectdb.py 可以:

To generate models for both tables and views in Django 2.0 and below, you have to edit the Django source code. In Django 2.0, change line 57 in django/core/management/commands/inspectdb.py to:

tables_to_introspect = options['table'] or connection.introspection.table_names(cursor=cursor, include_views=True)

请注意,生成的模型不会包含设置了 primary_key = True 的字段,您将需要手动添加主键.

Beware that the generated models won't have fields with primary_key=True set, you will need to add primary keys manually.

这篇关于Django 1.8 inspectdb命令根据文档看不到PostgreSQL视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆