Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for PostgresViewModel #198

Open
abmohan opened this issue Feb 3, 2023 · 2 comments
Open

Documentation for PostgresViewModel #198

abmohan opened this issue Feb 3, 2023 · 2 comments
Labels

Comments

@abmohan
Copy link
Contributor

abmohan commented Feb 3, 2023

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

@Photonios
Copy link
Member

Photonios commented Apr 4, 2023

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.

@abmohan
Copy link
Contributor Author

abmohan commented Apr 7, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants