ViewedModels
Viewed Models is a simple way to add PostGres “views” which look to Django just like Models. This lets us go outside the Django ORM to create views or materialized views to harness the power of PostGreSQL without losing the power to access that data using the ORM. It is very probably compatible with other DBMS but tested only with PG.
Installation
For production:
pip install django-viewedmodels
For development:
pip install -e git+https://github.com/joshbrooks/django_viewedmodels#egg=viewedmodels
Usage
A model which uses this framework should inherit from ViewedModel
The model requires an “sql” method which returns the sql required to create view
The model also requires a “dependencies” attribute. These are useful in generating table names within the SQL statement as well as dependency resolution.
The model also requires fields specified in the standard Django way. Foreign keys should work fine.
Foreign Keys: For ForeignKey(myApp.MyModel') we need to have a field mymodel_id returned from the SQL.
Every Django model (including these ViewedModels) requires an ‘id’ field. We can fake this by including row_number() OVER () AS id somewhere (usually as the first item) in our SELECT statement.
Example
from django.db import models
from django.contrib.postgres.fields import JSONField
from viewedmodels.models import ViewedModel
from viewedmodels.helpers import dependency_lookup
class ActivityCommitment(ViewedModel):
# Declaring dependencies for table lookup in the query and orderly build/ teardown of the views structure if
# there is inheritance between ViewedModel instances
dependencies = (
('aims', 'Activity'),
('aims', 'AidTypeCategory'),
('iati', 'Transaction'),
('aims', 'AidType'),
('aims', 'TransactionValueUSD')
)
@classmethod
def sql(cls):
tables = dependency_lookup(
cls.dependencies) # This returns a lookup for 'app_model' string to the true table name
return '''
SELECT
-- Columns { id, activity_id, aidtypecategory_id, dollars } reflect our field definition for the model
ROW_NUMBER() OVER () id, --Note how an id field is autogenerated for Django's sake
{aims_activity}.remote_data_id activity_id, -- This will be used as a ForeignKeyField, Django requires _id by default
{aims_aidtypecategory}.code aidtypecategory_id, -- Another fk field
{aims_transactionvalueusd}.dollars dollars
FROM
{aims_activity},
{aims_transactionvalueusd},
{iati_transaction},
{aims_aidtype},
{aims_aidtypecategory}
WHERE
--... --truncated for brevity
'''.format(**tables)
# id = models.AutoField(primary_key=True)
activity = models.ForeignKey('aims.Activity') # Represented as 'activity_id' in the query above
aidtypecategory = models.ForeignKey(
'aims.AidTypeCategory') # Represented as 'aidtypecategory_id' in the query above
dollars = models.FloatField()
Metadata
Release files for django-viewedmodels 0.1.26
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_viewedmodels-0.1.26.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_viewedmodels-0.1.26-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 20.0 kB
Release files / django_viewedmodels-0.1.26.tar.gz
| Download URL | django_viewedmodels-0.1.26.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0319ccb1ebb7c0cd77d15b51a4793e6c044fec6e1bff9774f80d920c13c9d0c
|
|
BLAKE2b-256 checksum How to use checksums |
c5d5d4aeb0899e8dd383365387b8494c275e5489ab22267ab7a3f85f3b1062c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_viewedmodels-0.1.26-py2.py3-none-any.whl
| Download URL | django_viewedmodels-0.1.26-py2.py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
2a54edac4b5533b9cc6b3e69b93b8d6697968214fd46a32929e2280731e4ca65
|
|
BLAKE2b-256 checksum How to use checksums |
3519d685633a23c706cf1223a4a44983870886fa6820e29117368722f1b8b069
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |