Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I using as module calling generate method and pass in disable_timestamp=True, the timestamp will still be displayed #2346

Open
Airpy opened this issue Mar 15, 2025 · 0 comments

Comments

@Airpy
Copy link

Airpy commented Mar 15, 2025

from datamodel_code_generator import (
    DataModelType,  # 注意:DataModelType 不在 __all__ 中定义,可能在未来的版本中发生变化
    InputFileType,
    PythonVersion,
    generate,
)

generate(
            json_str,
            input_file_type=InputFileType.Json,  # JSON类型
            output=model_file,  # 输出文件路径
            output_model_type=output_model_type,  # 输出模型类型
            # 这边datamodel_code_generator有bug(https://github.com/koxudaxi/datamodel-code-generator/issues/1977)
            # 将allow_extra_fields设置为True, 目的让生成的 Pydantic 模型包含所有字段
            allow_extra_fields=self.extra == "allow",  # 是否禁止额外字段
            target_python_version=PythonVersion.PY_311,  # 目标Python版本
            class_name=main_class_name,  # Pydantic模型类名(根类)
            use_schema_description=True,  # 使用Schema描述
            use_field_description=True,  # 使用字段描述
            field_constraints=True,  # 使用字段约束
            snake_case_field=True,  # 使用蛇形命名法
            use_standard_collections=True,  # 使用标准集合类型
            use_union_operator=True,  # 使用 | 而不是 Union
            use_double_quotes=True,  # 使用双引号
            disable_timestamp=True,  # 生成的文件头不显示timestamp
            enable_version_header=True,
        )
# generated by datamodel-codegen:
#   filename:  <stdin>
#   timestamp: 2025-03-15T05:19:22+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class Geo(BaseModel):
    lat: str
    lng: str


class Address(BaseModel):
    street: str
    suite: str
    city: str
    zipcode: str
    geo: Geo


class Company(BaseModel):
    name: str
    catch_phrase: str = Field(..., alias="catchPhrase")
    bs: str


class GetUserWithNormalUser(BaseModel):
    id: int
    name: str
    username: str
    email: str
    address: Address
    phone: str
    website: str
    company: Company
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant