3 ms·
Are there any sensible ways to do this with Django? Ideally with migrations to create and update the views.
by j4mie 7y ago
Are there any sensible ways to do this with Django? Ideally with migrations to create and update the views.
- mariocesar 7y 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.
- mariocesar 7y agoHere is an old article that I used to implement my own https://www.fusionbox.com/blog/detail/using-materialized-views-to-implement-efficient-reports-in-django/643/ https://www.fusionbox.com/blog/detail/using-materialized-vie...
- photonios 7y agoI am working on this! [1] Albeit slowly because it is not my full time job. What I have so far is support in the schema editor and very basic migration support. The underlying query is defined as a meta option on the model. [1] https://github.com/SectorLabs/django-postgres-extra/ https://github.com/SectorLabs/django-postgres-extra/
- djrobstep 7y agoIf you use a migration tool that works at the database level rather than the model level, the the migration handling of views will be automatically supported. Last time I tried to do model a database view in Django it was painful, but support may have improved since. But worst case you can drop down to a vanilla query.