Skip to main content

Django models backed by database views

Project description


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


  • pip install django_view_models
  • add django_view_models to INSTALLED_APPS
  • define view models (see below)
  • python 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()

        SELECT id, title
        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.0.tar.gz (5.0 kB view hashes)

Uploaded source

Built Distribution

django_view_models-0.0.0-py3-none-any.whl (10.9 kB view hashes)

Uploaded py3

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