How to add a database view to Django admin?

April 2020

Sometimes we want to display data which come from a database view and not from a Django model. It could be a view calculated by a stored procedure. It is super easy to do that.

We just need to declare the model corresponding to the view in our file but with the specific keyword managed=False in the class Meta information

class ProductSummary(models.Model):
    month = models.DateField()
    nbProductByMonth = models.IntegerField()
    class Meta:
        managed = False
        db_table = 'productsummary_view'

We declare a new model with fields month and nbProductByMonth and we tell Django to not create the table with the instruction managed=False

We also indicate the existing table name in the database: productsummary_view

Now you can add the table in the Admin as usual by adding


in your file

So in this previous example, we assume that we have a stored procedure (or whatever) which creates and manages this table.

Christophe Surbier