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.2.tar.gz
.
File metadata
- Download URL: django_view_models-0.0.2.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3fa28126295a5c6fe92f16b072054a404f1fc838a3f1a102b09e7f451882761 |
|
MD5 | 78865935e00603e47b1e6930968a8a78 |
|
BLAKE2b-256 | f9273f9bd04c3bbecf1254cc53c54ddf775c8441e0252947fcee2b559719e9fd |
File details
Details for the file django_view_models-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: django_view_models-0.0.2-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff4aa80d371b96a9a80b7be552f34e5fa971c738e3e2fa7a9a55283f00741ec0 |
|
MD5 | 987c8d475c348678ac1cf3dab61166bf |
|
BLAKE2b-256 | 5e082fbb7a829bda2228df3c1d02cd303fc90a9f32686fd9663ee2c45d60e201 |