Django 1.8 inspectdb command doesn't see PostgreSQL views as per documentation

六眼飞鱼酱① 提交于 2019-12-23 09:37:27

问题


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.

Here's the version output:

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

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)

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.

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


回答1:


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

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

If you want inspectdb to generate models for all tables and views by default, you have to edit the source to Django. 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)

Also, beware that the model won't have a field with primary_key=True set, you will need to add a primary key manually.




回答2:


In recent releases, there is an option to include views by passing it to the command as follows (both tables and views will be inspected):

python manage.py inspectdb --include-views > my_models.py


来源:https://stackoverflow.com/questions/30493610/django-1-8-inspectdb-command-doesnt-see-postgresql-views-as-per-documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!