GithubHelp home page GithubHelp logo

laroo / django-restapi-engine Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 270 KB

A simple Django database engine that interfaces with any RestAPI and perform basic CRUD actions

License: MIT License

Python 100.00%
database django python rest-api

django-restapi-engine's Introduction

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 [
            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 use django_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

django-restapi-engine's People

Contributors

laroo avatar pre-commit-ci[bot] avatar

Stargazers

 avatar  avatar Shekhroz avatar  avatar Marcos Ottonello avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.