Skip to main content

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 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 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

dbviews_django-0.1.1.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

dbviews_django-0.1.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file dbviews_django-0.1.1.tar.gz.

File metadata

  • Download URL: dbviews_django-0.1.1.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

Hashes for dbviews_django-0.1.1.tar.gz
Algorithm Hash digest
SHA256 795789034495976fcac4efda88ae5a9a9360ee8a538941159e6de3eab892fd1c
MD5 60dc9d54a30e41997a961de0db64bef2
BLAKE2b-256 9819a1010bcffd4d269ff29573cf74eae0cc355289420832027f3bf2c0006d9b

See more details on using hashes here.

File details

Details for the file dbviews_django-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: dbviews_django-0.1.1-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

Hashes for dbviews_django-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d7869b7735998a6507f6b838f43a6c951c3bd9f7a855828fd36c2854e695fbf1
MD5 edc234bbe6b39e66e50d3b392d03f0a5
BLAKE2b-256 ad67761e03cb32f79b79c3ca5baf16524b6f64dd7111ffc5f1be4f33d33891fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page