Hacker News new | ask | show | jobs
by mariocesar 2351 days ago
> Are there any sensible ways to do this with Django? Ideally with migrations to create and update the views.

There are apps to integrate PG views as django models, however is kind of trivial to do it yourself.

1. Create a migration that runs the SQL to create the view like ```create VIEW active_users AS SELECT rest of view ... `

2. Create a model mapping all the fields you want to use, add the Meta.table_name with the name of the view, and Meta.managed=False to avoid adding this to your migrations in the future.

Use it as any other normal Model.

Just that

About the updates with migrations I think if you already are thinking on using VIEWS, you could just create migrations that drop and recreate it when you needed. Is what I do, and is very little work for that.

1 comments

Here is an old article that I used to implement my own https://www.fusionbox.com/blog/detail/using-materialized-vie...