Django 1.8 inspectdb team doesn't see PostgreSQL view as per documentation

I have a Django 1.8 application with a PostgreSQL database. I run django inspectdb from the command line to test models for views, but the views do not appear in the model output file.

Here's the output of the version:

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

And here is 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 PostgreSQL views to output to Django 1.8.2 inspectdb?

+6
source share
2 answers

As in Django 1.10, you can simply name the individual view as a parameter of your inspectdb :

 $ python well/manage.py inspectdb hospitalizations_over_30_days 

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

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

In addition, be careful that the model will not have a field with primary_key=True , you will need to add the primary key manually.

+1
source

In the latest releases, it is possible to include views, passing them to the team as follows (both tables and views will be checked):

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

All Articles