Support of database views and materialized views in django web framework.
Project description
Usage
Discover how to define and utilize database views and materialized views seamlessly within your Django project.
Installation
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
Defining Views
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
Here's an example:
# 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()
Applying Migrations
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
Contributing
Contributions are welcome!
If you encounter any issues or have feature requests, please don't hesitate to submit them on GitHub.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dbviews_django-0.1.2.tar.gz
.
File metadata
- Download URL: dbviews_django-0.1.2.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.9.19 Linux/6.5.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6124bfbcd2e3d5b4631c9124d3b5659591bbf8f28c09e5a753b4d80350be0b7f |
|
MD5 | 4b44265996ded981808809cd6e68a440 |
|
BLAKE2b-256 | 73d9a30f3a0ab65413e5467f5128fb3e0b4a2bb59257cb6885f78b82f2914ce7 |
File details
Details for the file dbviews_django-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: dbviews_django-0.1.2-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.9.19 Linux/6.5.0-1017-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9cc508aac47ca3ea338f699df72fe1c196b812e3a36a0379ccd9cff5c91814c |
|
MD5 | 96f5f049d177e0d5676e11c3131b24e4 |
|
BLAKE2b-256 | a76153f4ccc4c93e26e6f78d0fd68f51fd3badc86358ddf6ef664cef6da0b8f6 |