Files
皓月归尘 1c316594f5 feat(generate): 优化代码生成逻辑
- 新增公共字段配置,统一处理常见字段的生成规则
- 修复模板中的一些错误,如变量名、函数名等
- 优化代码结构,提高可读性和可维护性
2025-07-01 23:40:43 +08:00

45 lines
1.6 KiB
Django/Jinja

# _*_ coding : UTF-8 _*_
# @Time : {{ current_time }}
# @UpdateTime : {{ current_time }}
# @Author : {{ author }}
# @File : {{ table_name }}.py
# @Comment : 本程序用于{{ table_comment }}模型
from tortoise import fields
from models.common import BaseModel
class {{ class_name }}(BaseModel):
"""
{{ table_comment }}模型
"""
{% for column in columns if column.is_common == false %}
{%- set params = [] %}
{%- if column.max_length is not none %}{% set params = params + ["max_length=" ~ column.max_length] %}{% endif %}
{%- if column.is_nullable %}{% set params = params + ["null=True"] %}{% endif %}
{%- if column.is_unique %}{% set params = params + ["unique=True"] %}{% endif %}
{%- if column.default is not none %}{% set params = params + ["default=" ~ column.default] %}{% endif %}
{%- if column.column_comment %}{% set params = params + ['description="' ~ column.column_comment ~ '"'] %}{% endif %}
{%- if column.column_name %}{% set params = params + ['source_field="' ~ column.column_name ~ '"'] %}{% endif %}
{{ column.python_name }} = fields.{{ column.field_type }}({{ params | join(", ") }})
"""
{{ column.column_comment }}
{%- if column.max_length is not none %}
- 最大长度为 {{ column.max_length }} 个字符
{%- endif %}
- 映射到数据库字段 {{ column.column_name }}
{%- if column.is_nullable %}
- 可为空
{%- endif %}
{%- if column.default is not none %}
- 默认值:{{ column.default }}
{%- endif %}
"""
{% endfor %}
class Meta:
table = "{{ table_name }}"
table_description = "{{ table_comment }}"