Comments (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)
- Enum is not being generated from JSON Schema from URL HOT 1
- Dataclass code gen error: TypeError: non-default argument 'XYZ' follows default argument
- "A Parser can not resolve classes" error when generating code from an OpenAPI spec HOT 2
- Code generation crashes with "black.parsing.InvalidInput: Cannot parse" HOT 1
- hostname string always generated with constr with pydantic2
- Regex pattern in JSON schema results in PyLance error
- PydanticV2 allows extra fields _without_ `--allow-extra-fields` option
- AttributeError: 'FieldInfo' object has no attribute '<EnumName>'
- New command line flag -add-v1-to-imports
- Union modes in Pydantic V2
- JSON Schema Discriminator With External Reference Support
- When generating pydantic with jsonData, an error occurred: "Invalid file format"
- Schemas with the same name in the path do not create valid models
- Missing Imports When Model Is Changed as a Discriminator
- One `Literal` generated from `discriminator.mapping` doesn't have default
- Models are not generated from OpenAPI 3.0.1
- Remove linters from package dependency HOT 1
- Generate Annotated types instead of root types for string constraints
- Add Swift codable structs support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datamodel-code-generator.