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
toINSTALLED_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
Release history Release notifications | RSS feed
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 django_view_models-0.0.0.tar.gz
.
File metadata
- Download URL: django_view_models-0.0.0.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c00dcb925db3704220e1c6353a80722ed6b6cd213e0420cb06225814bbf9ad0 |
|
MD5 | 049835429e20e2e0eb323b38e0cc10d7 |
|
BLAKE2b-256 | 79d30a7d1d73ce4320e214f088b7b17d8d064c887d9d8b006a1f37d0e2bf1264 |
File details
Details for the file django_view_models-0.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_view_models-0.0.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03285b376aca41415966e83e4dd6e7e21f21ffb03974229c2ba2ffb86cc8c9f1 |
|
MD5 | 92343ca9db5bb530531a253049506f8a |
|
BLAKE2b-256 | 6e483de222de34485f3f0172ee847b2c46fcc14ee71877dbfefeb8884514493b |