Giter Club home page Giter Club logo

Comments (1)

devmonkey22 avatar devmonkey22 commented on July 1, 2024 1

I know this was mentioned in the original PR #1735 and also brought up in #1944 (but closed), I have a similar need. I can't modify the source system to properly generate timezone strict output (all datetimes are already generated in UTC, but don't serialize to JSON that keeps the AwareDatetime happy). Datetimes that are in ISO 8601 format like "2024-05-29T18:00:29.526990Z" are fine with AwareDatetime at least. But I can't guarantee.

As a workaround to consume the JSON, while I dislike it, I currently have a script that calls datamodel_code_generator.codegen.generate programmatically instead of via direct CLI, so I can monkeypatch the datamodel_code_generator.model.pydantic_v2.types.IMPORT_AWARE_DATETIME value before generating like:

    # Patch to use naive datetime instead of default AwareDatetime
    from datamodel_code_generator.imports import Import
    from datamodel_code_generator.model.pydantic_v2 import types as pydantic_v2_types
    pydantic_v2_types.IMPORT_AWARE_DATETIME = Import.from_full_path('pydantic.NaiveDatetime')

    codegen.generate(
        ...
    )

Then my outputs properly use NaiveDatetime like I want.

Similarly, if I wanted to use the original standard datetime.datetime, just use Import.from_full_path('datetime.datetime')

Alternatively, I could have patched/overridden the pydantic_v2.types.DataTypeManager class and its type_map_factory method, but that seemed more complicated to subclass and inject into the generate/parser.

Obviously long-term, it would be great to have an official way to customize.

from datamodel-code-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.