This article presents an open-source library that builds automatically API services for any model defined in a Django application. Once the Dynamic API library is installed, any model enabled in the configuration becomes manageable via a DRF interface secured by JWT tokens. The sources, published on GitHub under the MIT License, can be used in commercial projects or eLearning activities. Thanks for reading!
This library is basically an abstract layer that connects any app model with DRF without writing any code. The coding pattern uses dynamic routing (no model name specialization) and the dynamic imports of the model definition at runtime.
A quick overview of this tool can be found on YouTube - Dynamic API
β¨ Library Core Features
The product can be integrated with any Django project using PIP:
$ pip install django-dynamic-api
Once the library is installed, we should be able to activate API services on top of any app model in no time.
β API engine provided by DRF
β Secured by JWT Tokens (mutating requests)
β Minimal Configuration (single line in the config)
β GET requests are public
β Update, Delete, and Creation requests are protected
β¨ How to use the product
This section presents all the necessary steps to code a simple Django project with a production-ready API provided by the Dynamic API library.
The superuser credentials are required later in API usage.
At this point, we have a basic Django starter ready to be used and extended with more features. Let's move on with our tutorial and code a simple app that defines a Book model.
π Step #5 - Create a new APP
$ django-admin startapp app1
This command executed in the root of the project will create a skeleton app. In order to make it more useful, we will define a simple model, managed later by the Dynamic API library.
π Step #6 - Configure the new app and migrate the database
Each time a Django project got a new app, the configuration should be updated in order to actually use it in the code:
INSTALLED_APPS=["django.contrib.admin","django.contrib.auth","django.contrib.contenttypes","django.contrib.sessions","django.contrib.messages","django.contrib.staticfiles","app1",# <-- NEW
]
The migration step is also mandatory for all the new apps:
π Step #8 - Update APPs section to include the library
INSTALLED_APPS=[...'django_dyn_api',# Django Dynamic API # <-- NEW
'rest_framework',# Include DRF # <-- NEW
'rest_framework.authtoken',# Include DRF Auth # <-- NEW
...]
At the end of the settings file, we need to add a new section used by the DYNAMIC Api module: