django api generator
v1.0.18
簡單的工具,以最小的努力在DRF上生成安全的API-由應用程序生成器積極支持。
有關完整的功能和長期支持,請查看Dynamic Django ,這是一個功能強大的起動器,它結合了:
特徵
DRF提供的API engineJWT Tokens保證(突變請求)Minimal Configuration (每個模型的配置中的單行)Handles any modelCRUD訪問邏輯:READ是公開的(所有項目,逐件獲取物品)Mutating requests受JWT Tokens保護
步驟#1-
Install the package
$ pip install django-api-generator
// OR
$ pip install git+https://github.com/app-generator/django-api-generator.git步驟#2-
Update Configuration,包括新應用程序
INSTALLED_APPS = [
'django_api_gen' , # Django API GENERATOR # <-- NEW
'rest_framework' , # Include DRF # <-- NEW
'rest_framework.authtoken' , # Include DRF Auth # <-- NEW
]步驟#3-在
core/settings.py(api_generator部分)中Register the model
該示例代碼假定app1存在,並且模型Book已定義和遷移。
API_GENERATOR = {
# pattern:
# API_SLUG -> Import_PATH
'books' : "app1.models.Book" ,
}
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES' : [
'rest_framework.authentication.SessionAuthentication' ,
'rest_framework.authentication.TokenAuthentication' ,
],
}步驟#4-
Migrate DB並創建DRF使用的表
$ python manage.py makemigrations
$ python manage.py migrate步驟#5-
Generate API
$ python manage.py generate-api
// OR
$ python manage.py generate-api -f # supress confirmation (forcing mode)該代碼是在項目根部的api文件夾下生成的。在每次迭代中, API代碼被覆蓋。
步驟#6-
Update routing,包括API
from django . contrib import admin
from django . urls import path , include # <-- UPD: 'include` directive
from rest_framework . authtoken . views import obtain_auth_token # <-- NEW
urlpatterns = [
path ( "admin/" , admin . site . urls ),
path ( "api/" , include ( "api.urls" )), # <-- NEW
path ( 'login/jwt/' , view = obtain_auth_token ), # <-- NEW
] 步驟#7-
Use API
如果託管模型是Books ,則API接口為/api/books/ ,並且所有CRUD方法都可以使用。
注意:對於突變請求,
JWT Token由http://localhost:8000/login/jwt/route(用戶應該存在)。

AppSeed提供的更多開發人員工具Email和Discord尋求支持Django API生成器 -應用程序提供的開源庫