8.7 KiB
8.7 KiB
:::tip 本章概要 代码生成器 JSON 配置文件的格式说明,包括模块名称、字段属性、字典编码等配置项的详细解释。 :::
配置文件详解
代码生成器支持通过 JSON 配置文件驱动生成。配置文件可通过 CLI --output 导出,也可手动编写,然后用 --config 加载生成。
导出配置
# 从数据库表解析并导出
python generator.py tornado_example --dry-run --output config.json
# 生成代码的同时保存配置
python generator.py tornado_example --output config.json
使用配置生成
python generator.py --config config.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_name |
str | 是 | 模块名(小写下划线),用于目录名和文件名 |
module_comment |
str | 是 | 模块中文名,用于注释和菜单名 |
model_class_name |
str | 是 | 模型类名(帕斯卡命名) |
permission_prefix |
str | 否 | 权限前缀,默认 sys:{app_name} |
display_field |
str | 否 | 显示字段,默认 name |
primary_key |
str | 否 | 主键字段,默认 id |
has_sort |
bool | 否 | 有排序字段,默认 false |
has_status |
bool | 否 | 有状态字段,默认 false |
has_unique_code |
bool | 否 | 有唯一编码字段,默认 false |
has_export |
bool | 否 | 有导出功能,默认 false |
has_image_field |
bool | 否 | 有图片字段,默认 false |
has_rich_text_field |
bool | 否 | 有富文本字段,默认 false |
fields |
list | 是 | 字段配置列表 |
字段配置项
每个字段对象包含:
基础属性
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
name |
str | 是 | 字段名(snake_case) |
comment |
str | 否 | 字段注释 |
db_type |
str | 是 | SQLAlchemy 类型 |
db_args |
str | 否 | 类型参数(如 "100") |
form_type |
str | 否 | Pydantic 类型(str/int/float/bool) |
max_length |
int | 否 | 最大长度 |
约束属性
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
nullable |
bool | false |
是否可空 |
required |
bool | true |
是否必填(与 nullable 相反) |
default |
any | null |
默认值 |
展示控制
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
in_form |
bool | true |
出现在表单中 |
in_list |
bool | true |
出现在列表中 |
editable |
bool | true |
可编辑 |
filterable |
bool | false |
作为查询条件 |
filter_type |
str | 模糊查询 |
筛选类型(精确匹配/模糊查询) |
searchable |
bool | false |
可搜索 |
功能标识
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
is_image |
bool | false |
图片字段 |
is_rich_text |
bool | false |
富文本字段 |
is_status |
bool | false |
状态字段 |
is_sort |
bool | false |
排序字段 |
选项属性
| 字段 | 类型 | 说明 |
|---|---|---|
choices |
list | [[value, label], ...] 元组列表 |
dict_code |
str | 数据字典编码 |
min_value |
int | 最小值 |
max_value |
int | 最大值 |
完整示例
{
"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 类型 |
|---|---|
String |
String(需配合 db_args 指定长度) |
Text |
Text |
Integer |
Integer |
BigInteger |
BigInteger |
DateTime |
DateTime |
Date |
Date |
Time |
Time |
Float |
Float |
Numeric |
Numeric |
Boolean |
Boolean |
JSON |
JSON |
form_type 必须是以下值之一:str / int / float / bool
filter_type 必须是以下值之一:精确匹配 / 模糊查询
常用微调场景
修改模块中文名
{
"module_comment": "培训记录"
}
隐藏字段(不出现在列表/表单)
{
"name": "internal_code",
"in_form": false,
"in_list": false
}
添加自定义字段
在 fields 数组中追加新的字段配置。
修改筛选类型
{
"name": "name",
"filterable": true,
"filter_type": "精确匹配"
}
添加选项
{
"name": "level",
"choices": [[1, "初级"], [2, "中级"], [3, "高级"]],
"dict_code": "example_level"
}
总结
配置文件为 JSON 格式,包含模块名称、表名、字段属性、字典编码等信息。支持字段级别的精细配置(是否可搜索、是否在列表显示、字典编码映射等),可通过 Web UI 可视化编辑或手动修改 JSON 文件。