Materialized View support for the Django framework. Django (in general) does not support materialized views by the default
therefor migrations are not created automatically with ./manage.py makemigrations.
This library provides new manage.py command: ./manage.py migrate_with_views.
This command is to be used instead of the default one migrate.
This command automatically finds your materialized view models and keeps them up to date.
In case when materialized view is a parent view for another materialized view, use migrate_with_views command
in order to change query of parent materialized view.
migrate_with_views command finds all related materialized views and recreates them sequentially.
Contents
Requirements
django-materialized-view has been tested with:
- Django: 4.0, 4.1
- Python: 3.9, 3.10, 3.11
- Postgres >= 13
Installation
Via pip into a virtualenv:
pip install django-materialized-view
In settings.py add the following:
INSTALLED_APPS = (
...
'django_materialized_view'
)
Before running migrate:
python manage.py migrate
Then you can use new migrate command instead of the default one:
python manage.py migrate_with_views
This command will automatically begin interception of materialized view models, and proceed to create/delete/update your view on your DB if required.
Usage
-
Create class and inherit from
MaterializedViewModelEXAMPLE:
from django.db import models from django_materialized_view.base_model import MaterializedViewModel class MyViewModel(MaterializedViewModel): create_pkey_index = True # if you need add unique field as a primary key and create indexes class Meta: managed = False # if create_pkey_index=True you must add argument primary_key=True item = models.OneToOneField("app.ItemModel", on_delete=models.DO_NOTHING, primary_key=True, db_column="id") from_seconds = models.IntegerField() to_seconds = models.IntegerField() type = models.CharField(max_length=255) # ATTENTION: this method must be a staticmethod or classmethod @staticmethod def get_query_from_queryset(): # define this method only in case use queryset as a query for materialized view. # Method must return Queryset pass
-
Add materialized view query (You can create a materialized view either from Raw SQL or from a queryset)
-
Create materialized view from Raw SQL
- run django default
makemigrationscommand for creating model migrations if necessary:./manage.py makemigrations - run
migrate_with_viewscommand for getting your new sql file name and path:./manage.py migrate_with_views - you will get file path in your console
[Errno 2] No such file or directory: '.../app/models/materialized_views/sql_files/myviewmodel.sql' - please create SQL file and put it to this directory - create file on suggested path with suggested name
- run again django command
migrate_with_views:
this command will run the default./manage.py migrate_with_viewsmigratecommand and apply materialized views
- run django default
-
Create materialized view query from Queryset
- run django default
makemigrationscommand for creating model migrations if necessary:./manage.py makemigrations - add to your materialized view model the method
get_query_from_queryset:# ATTENTION: this method must be a staticmethod or classmethod @staticmethod def get_query_from_queryset(): return SomeModel.objects.all()
- run django command
migrate_with_views:
This command will run default./manage.py migrate_with_viewsmigratecommand and apply materialized views
- run django default
-
-
Use
refreshmethod to update materialized view data.- For updating concurrently:
MyViewModel.refresh() - For updating non-concurrently:
MyViewModel.refresh(concurrently=Fasle)
Note: All refreshes will be logged in to the model MaterializedViewRefreshLog:
class MaterializedViewRefreshLog(models.Model): updated_at = models.DateTimeField(auto_now_add=True, db_index=True) duration = models.DurationField(null=True) failed = models.BooleanField(default=False) view_name = models.CharField(max_length=255)
- For updating concurrently:
Development
-
Release CI triggered on tags. To release new version, create the release with new tag on GitHub
-
For integration with pytest add following fixture:
@pytest.fixture(scope="session") def django_db_setup(django_db_setup, django_db_blocker): with django_db_blocker.unblock(): view_processor = MaterializedViewsProcessor() view_processor.process_materialized_views()
Metadata
Release files for django-materialized-view 0.1.6.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-materialized-view-0.1.6.2.tar.gz | 38.2 kB | Without provenance | Details for django-materialized-view-0.1.6.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_materialized_view-0.1.6.2-py3-none-any.whl | Python 3 | none | any | Details for django_materialized_view-0.1.6.2-py3-none-any.whl |
Total release size: 58.2 kB
Release files / django-materialized-view-0.1.6.2.tar.gz
| Download URL | django-materialized-view-0.1.6.2.tar.gz |
|---|---|
| Size | 38.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b99de6ad219434b9a6bd31c2cec98de0e4085f279185c2b4e8657b63ee2b883c
|
|
BLAKE2b-256 checksum How to use checksums |
102b3ceb64cec478c449d74d03296a1dfed7f66b17f1af439fe71410e4f232a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / django_materialized_view-0.1.6.2-py3-none-any.whl
| Download URL | django_materialized_view-0.1.6.2-py3-none-any.whl |
|---|---|
| Size | 20.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
29b3fc859f11109f4b4476b9aaa5c54e66fe6cb22f4074d5295111dd75ae6ac1
|
|
BLAKE2b-256 checksum How to use checksums |
61857c735480d91beab280c96691c6fff8eb436a7c6ebc84285f0ce337bd222b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Security for django-materialized-view 0.1.6.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.