Skip to content
Advertisement

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

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:

JavaScript

And here’s what psql sees:

JavaScript

From the django 1.8.2 documentation:

JavaScript

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

Advertisement

Answer

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

JavaScript

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

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!

JavaScript

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:

JavaScript

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement