Skip to content

本章概要

代码生成器 JSON 配置文件的格式说明,包括模块名称、字段属性、字典编码等配置项的详细解释。

配置文件详解

代码生成器支持通过 JSON 配置文件驱动生成。配置文件可通过 CLI --output 导出,也可手动编写,然后用 --config 加载生成。

导出配置

bash
# 从数据库表解析并导出
python generator.py fastapi_example --dry-run --output config.json

# 生成代码的同时保存配置
python generator.py fastapi_example --output config.json

使用配置生成

bash
python generator.py --config config.json

跳过表解析,直接从配置文件读取生成参数。适用于:

  • 微调字段配置后重新生成
  • 表结构已不存在但配置保留
  • 跨项目复用配置

配置文件结构

json
{
    "app_name": "example",
    "module_comment": "案例",
    "model_class_name": "Example",
    "permission_prefix": "sys:example",
    "display_field": "name",
    "primary_key": "id",
    "has_sort": true,
    "has_status": true,
    "has_unique_code": true,
    "has_export": false,
    "has_image_field": false,
    "has_rich_text_field": true,
    "fields": [...]
}

顶层配置项

字段类型必填说明
app_namestr模块名(小写下划线),用于目录名和文件名
module_commentstr模块中文名,用于注释和菜单名
model_class_namestr模型类名(帕斯卡命名)
permission_prefixstr权限前缀,默认 sys:{app_name}
display_fieldstr显示字段,默认 name
primary_keystr主键字段,默认 id
has_sortbool有排序字段,默认 false
has_statusbool有状态字段,默认 false
has_unique_codebool有唯一编码字段,默认 false
has_exportbool有导出功能,默认 false
has_image_fieldbool有图片字段,默认 false
has_rich_text_fieldbool有富文本字段,默认 false
fieldslist字段配置列表

字段配置项

每个字段对象包含:

基础属性

字段类型必填说明
namestr字段名(snake_case)
commentstr字段注释
db_typestrSQLAlchemy 类型
db_argsstr类型参数(如 "100"
form_typestrPydantic 类型(str/int/float/bool)
max_lengthint最大长度

约束属性

字段类型默认值说明
nullableboolfalse是否可空
requiredbooltrue是否必填(与 nullable 相反)
defaultanynull默认值

展示控制

字段类型默认值说明
in_formbooltrue出现在表单中
in_listbooltrue出现在列表中
editablebooltrue可编辑
filterableboolfalse作为查询条件
filter_typestr模糊查询筛选类型(精确匹配/模糊查询
searchableboolfalse可搜索

功能标识

字段类型默认值说明
is_imageboolfalse图片字段
is_rich_textboolfalse富文本字段
is_statusboolfalse状态字段
is_sortboolfalse排序字段

选项属性

字段类型说明
choiceslist[[value, label], ...] 元组列表
dict_codestr数据字典编码
min_valueint最小值
max_valueint最大值

完整示例

json
{
    "app_name": "example",
    "module_comment": "案例",
    "model_class_name": "Example",
    "permission_prefix": "sys:example",
    "display_field": "name",
    "has_sort": true,
    "has_status": true,
    "has_unique_code": true,
    "has_image_field": true,
    "has_rich_text_field": true,
    "fields": [
        {
            "name": "name",
            "comment": "案例名称",
            "db_type": "String",
            "db_args": "100",
            "form_type": "str",
            "max_length": 100,
            "nullable": false,
            "required": true,
            "in_form": true,
            "in_list": true,
            "filterable": true,
            "filter_type": "模糊查询",
            "searchable": true
        },
        {
            "name": "code",
            "comment": "案例编码",
            "db_type": "String",
            "db_args": "50",
            "form_type": "str",
            "max_length": 50,
            "nullable": false,
            "required": true,
            "in_form": true,
            "in_list": true,
            "filterable": true,
            "filter_type": "精确匹配",
            "searchable": true
        },
        {
            "name": "type",
            "comment": "案例类型:1-类型1 2-类型2 3-类型3",
            "db_type": "Integer",
            "form_type": "int",
            "nullable": false,
            "required": true,
            "in_form": true,
            "in_list": true,
            "filterable": true,
            "filter_type": "精确匹配",
            "choices": [[1, "类型1"], [2, "类型2"], [3, "类型3"]],
            "dict_code": "example_type"
        },
        {
            "name": "status",
            "comment": "案例状态:1-正常 2-停用",
            "db_type": "Integer",
            "form_type": "int",
            "default": 1,
            "nullable": false,
            "required": true,
            "in_form": true,
            "in_list": true,
            "filterable": true,
            "filter_type": "精确匹配",
            "is_status": true,
            "choices": [[1, "正常"], [2, "停用"]],
            "dict_code": "example_status"
        },
        {
            "name": "sort",
            "comment": "排序",
            "db_type": "Integer",
            "form_type": "int",
            "default": 0,
            "nullable": false,
            "required": true,
            "in_form": true,
            "in_list": true,
            "is_sort": true
        },
        {
            "name": "cover",
            "comment": "封面图",
            "db_type": "String",
            "db_args": "255",
            "form_type": "str",
            "max_length": 255,
            "nullable": true,
            "required": false,
            "in_form": true,
            "in_list": true,
            "is_image": true
        },
        {
            "name": "content",
            "comment": "案例内容",
            "db_type": "Text",
            "form_type": "str",
            "nullable": true,
            "required": false,
            "in_form": true,
            "in_list": false,
            "is_rich_text": true
        },
        {
            "name": "remark",
            "comment": "备注",
            "db_type": "String",
            "db_args": "500",
            "form_type": "str",
            "max_length": 500,
            "nullable": true,
            "required": false,
            "in_form": true,
            "in_list": true,
            "filterable": true,
            "filter_type": "模糊查询"
        }
    ]
}

字段配置校验规则

db_type 必须是以下值之一:

合法值对应 SQLAlchemy 类型
StringString(需配合 db_args 指定长度)
TextText
IntegerInteger
BigIntegerBigInteger
DateTimeDateTime
DateDate
TimeTime
FloatFloat
NumericNumeric
BooleanBoolean
JSONJSON

form_type 必须是以下值之一:str / int / float / bool

filter_type 必须是以下值之一:精确匹配 / 模糊查询

常用微调场景

修改模块中文名

json
{
    "module_comment": "培训记录"
}

隐藏字段(不出现在列表/表单)

json
{
    "name": "internal_code",
    "in_form": false,
    "in_list": false
}

添加自定义字段

fields 数组中追加新的字段配置。

修改筛选类型

json
{
    "name": "name",
    "filterable": true,
    "filter_type": "精确匹配"
}

添加选项

json
{
    "name": "level",
    "choices": [[1, "初级"], [2, "中级"], [3, "高级"]],
    "dict_code": "example_level"
}

总结

配置文件为 JSON 格式,包含模块名称、表名、字段属性、字典编码等信息。支持字段级别的精细配置(是否可搜索、是否在列表显示、字典编码映射等),可通过 Web UI 可视化编辑或手动修改 JSON 文件。

小蚂蚁云团队 · 提供技术支持