dbviews-django is a Django package designed to simplify the management of database views and materialized views within Django projects. With dbviews-django, you can seamlessly integrate database views into your Django applications, allowing for easier maintenance and management of complex data structures.
- Define database views and materialized views using Django's migration framework.
- Integrate views seamlessly with your existing Django models and queries.
- Easily manage and update views alongside your Django application code.
- Support for both traditional database views and materialized views for improved performance.
- Compatible with Python 3.9 or higher and Django 4.x.
Discover how to define and utilize database views and materialized views seamlessly within your Django project.
Ensure you have Python 3.9 or higher and Django 4.x installed before proceeding with the installation.
You can install the views package via pip:
pip install dbviews-django
To define views in your Django project, you will first need to ensure that the dbviews
app is included in your project settings. You can do this by adding 'dbviews'
to the INSTALLED_APPS
list in your settings.py
file:
# settings.py
INSTALLED_APPS = [
...
'dbviews',
...
]
Once dbviews is included, you can define your views using the provided classes.
You can define your views in either models.py
or in a separate views.py
file located within a models
folder.
Here's how you can structure your Django app:
- yourapp/
models/
__init__.py
models.py
views.py
# models/views.py
from dbviews import views
class MyView(views.DbView):
"""
Define your view using DbView class.
"""
view_query = views.QueryField(query="SELECT * FROM my_table WHERE condition = true")
# Other fields....
class MyMaterializedView(views.DbMaterializedView):
"""
Define your materialized view using DbMaterializedView class.
"""
view_query = views.QueryField(query="SELECT * FROM my_table WHERE condition = true")
# Other fields....
In the above example, MyView and MyMaterializedView are defined as subclasses of DbView and DbMaterializedView respectively. The view_query attribute specifies the SQL query that defines the view's logic.
To make your models and views accessible from the root of the models folder, you need to import them in the init.py file:
# __init__.py
from .models import MyModel # Import your models
from .views import MyView # Import your views
To refresh the materialized views you can use refresh method.
MyMaterializedView.refresh()
After defining your views, you'll need to generate migrations to apply these changes to your database schema. Use Django's makemigrations
and migrate
command to generate migration files:
python manage.py makemigrations # This will create all the required migrations
python manage.py migrate # This will create all the views and models based on the migration
Contributions are welcome!
If you encounter any issues or have feature requests, please don't hesitate to submit them on GitHub.