<% schema_name = entity.name.capitalize() + 'Schema' create_schema_name = entity.name.capitalize() + 'Create' update_schema_name = entity.name.capitalize() + 'Update' relations = generator._process_relations(entity) %> from pydantic import BaseModel from typing import Optional, List from datetime import datetime class ${schema_name}(BaseModel): """ ${entity.description or schema_name + ' 返回模型'} 包含所有字段和关联字段 """ id: int % for field in entity.fields: ${field['name']}: ${'str' if field['type'] == 'String' else 'int' if field['type'] in ('Long', 'Integer') else 'bool' if field['type'] == 'Boolean' else 'datetime' if field['type'] == 'DateTime' else 'str'} % endfor # 关联字段 % for rel in relations: % if rel['type'] in ('one-to-many', 'many-to-many'): ${rel['name']}: List['${rel['target'].capitalize()}Schema'] = [] % elif rel['type'] == 'many-to-one': ${rel['name']}: Optional['${rel['target'].capitalize()}Schema'] = None % endif % endfor class Config: orm_mode = True schema_extra = { "example": { "id": 1, % for field in entity.fields: "${field['name']}": ${'"sample"' if field['type'] == 'String' else 1 if field['type'] in ('Long', 'Integer') else True if field['type'] == 'Boolean' else '"2023-01-01T00:00:00"' if field['type'] == 'DateTime' else '"sample"'}, % endfor } } class ${create_schema_name}(BaseModel): """ ${entity.description or create_schema_name + ' 创建模型'} 不包含只读字段 """ % for field in entity.fields: % if not hasattr(field, 'primary_key') or not field['primary_key']: ${field['name']}: ${'str' if field['type'] == 'String' else 'int' if field['type'] in ('Long', 'Integer') else 'bool' if field['type'] == 'Boolean' else 'datetime' if field['type'] == 'DateTime' else 'str'} % endif % endfor class ${update_schema_name}(BaseModel): """ ${entity.description or update_schema_name + ' 更新模型'} 所有字段都是可选的 """ % for field in entity.fields: % if not hasattr(field, 'primary_key') or not field['primary_key']: ${field['name']}: Optional[${'str' if field['type'] == 'String' else 'int' if field['type'] in ('Long', 'Integer') else 'bool' if field['type'] == 'Boolean' else 'datetime' if field['type'] == 'DateTime' else 'str'}] % endif % endfor