Schema Sprout connects to a third-party database to dynamically create models and views.
Project description
Django Schema Sprout
Django Schema Sprout is a django package that helps with generating models, serializers and views dynamically for unmanaged databases. Currently Django Schema Sprout supports only PostgreSQL (>=11.), Django (>=4.0, <5.0)
Installation
To install Django Schema Sprout, you can run:
pip install django-schema-sprout
Usage
Settings.py
Add django_schema_sprout
in installed apps in your settings.py
and add SchemaSproutDBRouter
to your database routers.
INSTALLED_APPS = [
# django_schema_sprout requires rest_framework and drf_yasg to be installed.
"rest_framework",
"drf_yasg",
...
"django_schema_sprout"
]
...
DATABASE_ROUTERS = [
...,
"django_schema_sprout.router.SchemaSproutDBRouter",
]
Initializing SchemaSprout
from django_schema_sprout.schema_sprout import SchemaSprout
my_db_sprout = SchemaSprout("name_of_unmanaged_db")
SchemaSprout
is Singleton based on parameters so initializing SchemaSprout
with same database name will return same instance.
Creating models
You can either create all models at once using create_models
.
create_models
takes one parameter:
readonly
bool (default:False
), specifying if created REST ViewSets should be readonly or not.
my_db_sprout.create_models(readonly=True)
To create one model at the time use create_model
.
create_model
takes 3 parameter:
table_name
- str, name of the table for which you want to create model.table_nspname
- str, schema where the table resides.readonly
bool (default:False
), specifying if created REST ViewSet should be readonly or not.
my_db_sprout.create_model(
table_name="my_table"
table_nspname="my_schema"
readonly=False
)
Including SchemaSprout ViewSets
In url.py
file, include urls from SchemaSprout.router
. Make sure that the models that you need were created before.
from django.contrib import admin
from django.urls import path, re_path, include
...
from django_schema_sprout.schema_sprout import SchemaSprout
urlpatterns = [
...,
path("api/", include(SchemaSprout("name_of_unmanaged_db").router.urls)),
...
]
Accessing created models
You can access created models, serializers and views by accessing SchemaSprout attributes:
table_name = "my_table"
schema_name = "my_schema"
obj_key = f"{schema_name}_{table_name}"
my_model = my_db_sprout.models.get(obj_key, None)
my_model_serializer = my_db_sprout.serializers.get(obj_key, None)
my_model_view = my_db_sprout.views.get(obj_key, None)
Contribution
Feel free to contriute or open an issue.
For local development:
- in your venv install dev dependencies
pip install -e ".[dev]"
- for testing install test dependencies
pip install -e ".[test]"
Running tests requires to have installed Postgres.
License
This project is licensed under the Apache License 2.0
Contact
For questions feel free to contact me: grumpy.miner.dev@gmail.com
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_schema_sprout-0.1.1.tar.gz
.
File metadata
- Download URL: django_schema_sprout-0.1.1.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77676c3f9773e4c8abc8341c0dc3ac00ec120b39c8259adbccb44fdf310b8edd |
|
MD5 | da70887207389aaec3ba72676866a3fa |
|
BLAKE2b-256 | 9cc6e7a46a7a3be6eb26ed7e88a83cd77af0150ef0b5a0693faef661bd5b626a |
File details
Details for the file django_schema_sprout-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_schema_sprout-0.1.1-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd0971711665a2d09da4f101aaf74af8abdbb68c5e6267e6e99c37c078736e4c |
|
MD5 | 78b60b3ab06ecfd360456af93db11f38 |
|
BLAKE2b-256 | 82430896b21873fe91dd05edcf800b92522af773f797a3e7b73100d8edfcb3a9 |