Skip to main content

Django models backed by database views

Project description

django_view_models

Designed for Postgres. May or may not work with other databases.

Installation/Usage

  • pip install django_view_models
  • add django_view_models to INSTALLED_APPS
  • define view models (see below)
  • python manage.py sync_view_models (run after every view definition change)

Defining View Models

You must define the fields on your model, and either define VIEW_DEFINITION or view_query on your model.

Note that just like regular models, django will add an integer primary key called id, unless you set primary_key=True on one of your fields.

from django.db import models
from django_view_models.models import ViewModel, MaterializedViewModel

class Book(models.Model):
    title = models.TextField()
    is_good = models.BooleanField()

# create a ViewModel with a manual query
class GoodBook(ViewModel):
    title = models.TextField()

    VIEW_DEFINITION = '''
        SELECT id, title
        FROM my_app.book
        WHERE is_good = true
    '''

# create a ViewModel, and use Django's ORM to create the query
class BadBook(ViewModel):
    title = models.TextField()

    def view_query():
        return Book.objects.filter(is_good=False).values('id', 'title')

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

django_view_models-0.0.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

django_view_models-0.0.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file django_view_models-0.0.1.tar.gz.

File metadata

  • Download URL: django_view_models-0.0.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10

File hashes

Hashes for django_view_models-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5a0ed326154d1c5425f072c3b8aedf12fdd8530541b2ad75329b7bc3674e0f3b
MD5 e47b33566169951e038e332b27b08f81
BLAKE2b-256 5f17e9e989cbc0946b5b1db1dcc5a55b7618fa96eef735e05b789458d2cf37fa

See more details on using hashes here.

File details

Details for the file django_view_models-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_view_models-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10

File hashes

Hashes for django_view_models-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7309f3e97a62f09585b3de7fa7fd83a1b021a9e583a494ed95017c9a25dcef7e
MD5 d622474047285d063b13845363001334
BLAKE2b-256 34d432beb54691743f3c60ece427c239a96fd3acfef09b98da905c819a70e516

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