Use any RestAPI as basic Django Database Engine
Project description
django-restapi-engine
Use any RestAPI as basic Django Database Engine
About
A simple Django database engine that interfaces with any RestAPI and perform basic CRUD actions
Usage
Installation
pip install git+https://github.com/laroo/django-restapi-engine.git@v0.1.1
Create RestAPI handler
Create a custom RestAPI handler that implements all methods from BaseRestApiHandler
from django_restapi_engine.rest_api_handler import BaseRestApiHandler
class MyCustomRestApiHandler(BaseRestApiHandler):
def list(self, *, model, columns, query):
return [
return {'id': 1`, 'title': 'some title'},
return {'id': 2`, 'title': 'another title'}
]
def get(self, *, model, pk, columns):
return {'id': 1`, 'title': 'some title'}
def insert(self, *, model, obj, fields, returning_fields):
return {'id': 3`}
def update(self, *, model, pk, values):
return 1
def delete(self, *, model, pk):
return
Django Database Configuration
In Django's settings.py
add a new database config after the default
connection
with the following settings:
ENGINE
: Tell Django to usedjango_restapi_engine
DEFAULT_HANDLER_CLASS
: Point to your custom RestAPI handler class created in previous step
Example:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
},
'restapi': {
'ENGINE': 'django_restapi_engine',
'DEFAULT_HANDLER_CLASS': 'module.location.of.MyCustomRestApiHandler'
}
}
Usage
# Create
todo = Todo(
user_id=123,
title="My new todo!",
completed=False
)
todo.save(using="restapi")
# Read
todo = Todo.objects.using('restapi').get(pk=1)
# Update
todo.title = "New title!"
todo.save(using="restapi")
# Delete
todo.delete(using="restapi")
# List
Todo.objects.using('restapi').all()
Django Admin Configuration
Create custom admin class that extends ModelAdmin
to point to the new database connection:
class RestApiModelAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.save(using='restapi')
def delete_model(self, request, obj):
obj.delete(using='restapi')
def get_queryset(self, request):
return super().get_queryset(request).using('restapi')
See example project
Example Project
See README.md in example_projects/jsonplaceholder_project
Limitations
There is no support for relationships like ForeignKey
and ManyToManyField
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
Close
Hashes for django_restapi_engine-0.1.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c081ed3d7ed0cbfd16c48b54f78618a1f6bc7bee945ecc8426f1c69f535ea3e0 |
|
MD5 | 8369016a3fdbc909d47700712fe4b476 |
|
BLAKE2b-256 | 36309c9fc4a595685c14ec92085db0c7c772b255d0da74d2b84aeddad2ee006d |
Close
Hashes for django_restapi_engine-0.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 882253b56be1d3391fa19a197c5fa47d9265260538fa1e067746a9d74731cc5d |
|
MD5 | 3885bda4fc4a5cdc86fa333ae8785b50 |
|
BLAKE2b-256 | 2d1196a67dae131323dc77dd602f3977d1256b49ed2c6d9d60bf99b793e8580a |