django-postgres-extra icon indicating copy to clipboard operation
django-postgres-extra copied to clipboard

Documentation for `PostgresViewModel`

Open abmohan opened this issue 2 years ago • 2 comments

Would love to see an example for PostgresViewModel.

I've tried reading through the code to figure out how to use it, but am unclear on the implementation.

E.g., something like what they have in https://github.com/xelixdev/django-pgviews would be great

abmohan avatar Feb 03 '23 22:02 abmohan

This is not documented because nobody has confirmed it's stable and I am personally not using these features right now so I can't guarantee their stability. There is also some missing functionality, such as the ability to change the backing query on a view.

The basics:

from psqlextra.models import PostgresModel, PostgresViewModel

class SomeModel(PostgresModel):
    name = models.TextField()

class MyView(PostgresViewModel):
   class ViewMeta:
       # alternatively, you can specify raw SQL here:
       # query = ("SELECT ...", (param1, param2))
       query = SomeOtherModel.objects.filter(name="test")
       
   # all fields returned by `query` must be specified as fields
   name = models.TextField()
from psqlextra.models import PostgresMaterializedViewModel

class MyMaterializedView(PostgresMaterializedViewModel):
   class ViewMeta:
       query = SomeOtherModel.objects.filter(name="test")
     
   # all fields returned by `query` must be specified as fields  
   name = models.TextField()
   
MyMaterializedView.refresh(concurrently=True)    

Use python manage.py pgmakemigrations to automatically create the views in your database. Changing the query is unfortunately not supported in the migrations right now, you'll have to do that manually.

Photonios avatar Apr 04 '23 06:04 Photonios

Thanks @Photonios. This is helpful. I've removed it from my project for now. Once I have some breathing room (hopefully in a few months), I'd be happy to add it back in and help with testing, documentation, and migrations at that point.

abmohan avatar Apr 07 '23 18:04 abmohan