Files
fastapi-best-architecture/backend/plugin/code_generator/templates/python/schema.jinja
T
Wu Clan 1a6aba6105 Optimize code generation data processing (#1020)
* Optimize code generation data processing

* Add SQL script generation

* Update code gen table scripts

* Update types

* Fix model jinja and type conversion
2026-01-20 18:27:42 +08:00

57 lines
1.9 KiB
Django/Jinja

{% set pd_types = models|map(attribute='pd_type')|list %}
{% set NEED_DATETIME = datetime_mixin or 'datetime' in pd_types %}
{% set NEED_DATE = 'date' in pd_types %}
{% set NEED_TIME = 'time' in pd_types %}
{% set NEED_TIMEDELTA = 'timedelta' in pd_types %}
{% set NEED_DECIMAL = 'Decimal' in pd_types %}
{% set NEED_UUID = 'UUID' in pd_types or 'str | UUID' in pd_types %}
{% if NEED_DATETIME or NEED_DATE or NEED_TIME or NEED_TIMEDELTA %}
from datetime import {% if NEED_DATETIME %}datetime{% endif %}{% if NEED_DATE %}{% if NEED_DATETIME %}, {% endif %}date{% endif %}{% if NEED_TIME %}{% if NEED_DATETIME or NEED_DATE %}, {% endif %}time{% endif %}{% if NEED_TIMEDELTA %}{% if NEED_DATETIME or NEED_DATE or NEED_TIME %}, {% endif %}timedelta{% endif %}
{% endif %}
{% if NEED_DECIMAL %}
from decimal import Decimal
{% endif %}
{% if NEED_UUID %}
from uuid import UUID
{% endif %}
from pydantic import ConfigDict, Field
from backend.common.schema import SchemaBase
class {{ schema_name }}SchemaBase(SchemaBase):
"""{{ doc_comment }}基础模型"""
{% for model in models %}
{{ model.name }}: {% if model.is_nullable %}{{ model.pd_type }} | None = Field(None, description='{{ model.comment }}'){% else %}{{ model.pd_type }} = Field(description='{{ model.comment }}'){% endif %}
{% endfor %}
class Create{{ schema_name }}Param({{ schema_name }}SchemaBase):
"""创建{{ doc_comment }}参数"""
class Update{{ schema_name }}Param({{ schema_name }}SchemaBase):
"""更新{{ doc_comment }}参数"""
class Delete{{ schema_name }}Param(SchemaBase):
"""删除{{ doc_comment }}参数"""
pks: list[int] = Field(description='{{ doc_comment }} ID 列表')
class Get{{ schema_name }}Detail({{ schema_name }}SchemaBase):
"""{{ doc_comment }}详情"""
model_config = ConfigDict(from_attributes=True)
id: int
{% if datetime_mixin %}
created_time: datetime
updated_time: datetime | None = None
{% endif %}