标准化接口-标准 CRUD

要对某个模型做增删改查、列表分页过滤、或拿字段元信息渲染表单时读这篇

06-二次开发 / API参考 / 标准化接口
_infolistshowaddeditdelete列表查询分页过滤filter操作符oprcteqrel_o_m元信息

标准 CRUD 接口

URL 中的 $view 为路由前缀(如 /pipeline_modelview/api),见 01-通用约定与路径对照.md

1. 元信息接口 _info

获取模型的字段定义、权限、过滤器等元信息,用于前端动态渲染表单。

GET  http://x.x.x.x/$view/api/_info

请求参数(Query):

参数 类型 必填 说明
form_data JSON string 可传入 {"pk": <id>} 获取指定记录的编辑数据

响应示例:

{
  "status": 0,
  "message": "success",
  "result": {
    "add_columns": [
      {
        "name": "name",
        "label": "名称",
        "description": "字段说明",
        "type": "String",
        "required": true,
        "unique": true,
        "ui-type": "input",
        "values": [],
        "default": ""
      }
    ],
    "edit_columns": [...],
    "show_columns": [...],
    "list_columns": [...],
    "search_filters": {
      "name": [
        {"name": "Contains", "operator": "ct"},
        {"name": "Equal to", "operator": "eq"}
      ]
    },
    "label_columns": {"name": "名称", "describe": "描述"},
    "description_columns": {"name": "请输入名称"},
    "add_fieldsets": [
      {"label": "基础信息", "fields": ["name", "describe"]},
      {"label": "高级配置", "fields": ["config"], "expanded": false}
    ],
    "edit_fieldsets": [...],
    "show_fieldsets": [...],
    "add_title": "添加Pipeline",
    "edit_title": "编辑Pipeline",
    "show_title": "查看Pipeline",
    "list_title": "Pipeline列表",
    "columns_info": {"name": {"type": "String", "length": 200}},
    "help_url": "",
    "route_base": "/pipeline_modelview/api/",
    "primary_key": "id",
    "label_title": "Pipeline",
    "cols_width": {"name": {"type": "ellip2", "width": 200}},
    "import_data": true,
    "download_data": true,
    "enable_favorite": true,
    "echart": false,
    "page_size": 100,
    "permissions": ["can_add", "can_show", "can_edit", "can_delete", "can_list"],
    "user_permissions": {"add": true, "edit": true, "delete": true, "show": true},
    "ops_link": [{"text": "查看日志", "url": "/web/log/{id}"}],
    "action": {
      "action_name": {
        "name": "action_name",
        "text": "按钮显示文字",
        "confirmation": "确认执行?",
        "icon": "fa-play",
        "single": true,
        "multiple": false
      }
    },
    "related": {
      "project": {"add_url": "/project_modelview/api/", "list_url": "/project_modelview/api/"}
    },
    "exist_add_args": {},
    "more_info": {},
    "data": {}
  }
}

说明:

  • 传入 pk 时返回对应记录数据(用于编辑页面回填),同时会校验编辑权限
  • 不传 pk 时返回新增表单的默认值和上下文
  • add_columns / edit_columns 中每个字段对象包含 ui-type(input / select / textArea / datePicker 等)供前端渲染
  • action 字段描述了可用的操作按钮(对应 action/multi_action 接口)
  • permissions 列出当前用户对该模块的权限列表
  • route_base 即视图的 $view 路径,返回值在 myapp/views/baseApi.py:937 拼接为 "/" + route_base.strip('/') + "/"

2. 列表查询 list

GET  http://x.x.x.x/$view/api/

请求参数(通过 form_data query 传递,值为 JSON 序列化后的字符串):

http://x.x.x.x/$view/api/?form_data=$json_value
参数 类型 必填 说明
page int 页码(从 0 开始),默认 0
page_size int 每页条数,默认由视图配置
order_column string 排序列名
order_direction string 排序方向:asc / desc
columns array 指定返回的列,如 ["name","id"]
filters array 过滤条件数组,见下方说明

filter 过滤条件格式:

{
  "filters": [
    {"col": "name", "opr": "ct", "value": "test"}
  ]
}

支持的过滤操作符:

操作符 说明 示例
sw Starts with 以…开头 {"col":"name","opr":"sw","value":"pipe"}
nsw Not Starts with 不以…开头
ew Ends with 以…结尾
new Not Ends with 不以…结尾
ct Contains 包含 {"col":"name","opr":"ct","value":"test"}
nct Not Contains 不包含
eq Equal to 等于 {"col":"status","opr":"eq","value":"online"}
neq Not Equal to 不等于
gt Greater than 大于 {"col":"id","opr":"gt","value":100}
lt Smaller than 小于
rel_o_m Relation 外键关联(多对一) {"col":"project","opr":"rel_o_m","value":1}
nrel_o_m No Relation 排除外键关联
rel_m_m Relation as Many 多对多关联
eqf Filter with function 函数过滤
inf Field in function list 字段值在函数返回列表中

响应示例:

{
  "status": 0,
  "message": "success",
  "result": {
    "count": 100,
    "data": [
      {"id": 1, "name": "pipeline-demo", "project": {"id": 1, "name": "demo-project"}, "status": "online"}
    ]
  }
}

请求示例(查询 name 包含 "test" 的记录,按 id 降序,取第 1 页每页 10 条):

GET /pipeline_modelview/api/?form_data={"page":0,"page_size":10,"order_column":"id","order_direction":"desc","filters":[{"col":"name","opr":"ct","value":"test"}]}

3. 获取单条记录 show

GET  http://x.x.x.x/$view/api/<id>

路径参数: id(int)记录主键。

响应示例:

{
  "status": 0,
  "message": "success",
  "result": {
    "id": 1,
    "name": "pipeline-demo",
    "describe": "示例任务流",
    "project": {"id": 1, "name": "demo-project"}
  }
}

4. 新增记录 add

POST  http://x.x.x.x/$view/api/
Content-Type: application/json

请求体(JSON):

{
  "name": "pipeline-new",
  "describe": "新任务流",
  "project": "1"
}

响应示例:

{
  "status": 0,
  "message": "success",
  "result": {
    "id": 2,
    "name": "pipeline-new",
    "describe": "新任务流",
    "project": {"id": 1, "name": "demo-project"}
  }
}

5. 更新记录 edit

PUT  http://x.x.x.x/$view/api/<id>
Content-Type: application/json

路径参数: id(int)记录主键。

请求体(JSON):

{
  "describe": "修改后的描述"
}

响应示例:

{
  "status": 0,
  "message": "success",
  "result": {"id": 2, "name": "pipeline-new", "describe": "修改后的描述"}
}

6. 删除记录 delete

DELETE  http://x.x.x.x/$view/api/<id>

路径参数: id(int)记录主键。

响应示例:

{
  "status": 0,
  "message": "success",
  "result": {"id": 2}
}
最后更新 2026-06-30完整文档以官方仓库为准:GitHub Wiki