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生成器 -应用程序提供的开源库