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.
Motivation
To interact with rest API's I was creating scripts using curl or Postman UI. This is quite
cumbersome and often fails as it's missing proper validation and a easy UI.
With Django admin it's possible to create a customizable CRUD interface in a few simple lines of code, but it only works on database backends. Leveraging Django admin for CRUD operations by defining a basic database engine that interfaces with any RestAPI.
Usage
Installation
Stable version:
pip install django-restapi-engine
Development version:
pip install git+https://github.com/laroo/django-restapi-engine.git@main
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 [
{'id': 1, 'title': 'some title'},
{'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_engineDEFAULT_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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_restapi_engine-0.2.0.tar.gz.
File metadata
- Download URL: django_restapi_engine-0.2.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac524baa9381c8628a45703e093729b9219701c4b49c5a62d1aad86f3f5c2e57
|
|
| MD5 |
2cf26cb9e5d2559cd6999bf481a35911
|
|
| BLAKE2b-256 |
734c8f161e604e5d9ef20c6bffbb78d80df1cd5f641fc18e803250be1e4d7cd2
|
File details
Details for the file django_restapi_engine-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_restapi_engine-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.28.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89bf10e5b9d40370cb9df27c40845aa1061a3144cd379909c8daae685e065156
|
|
| MD5 |
33a65e2b0c84c1ed9f098e421c1e28ee
|
|
| BLAKE2b-256 |
73747c2c6ed349cf249ec0050ec7cd26d726e84830766ffc28fd3256470e257c
|