You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I love that this codegen tool exists! We currently rely on models generated with this library, but we need to support compatibility with both Pydantic v1 and v2. I'm struggling with how to do this.
Describe the solution you'd like
I'd like datamodel-code-generator to be able to generate "v1" Pydantic models, but in a way that also forwards-compatible with Pydantic 2.
This can be implemented with the following:
# We use Pydantic 1.x for backwards compatibility. We import it in a# way that is also forwards compatible with Pydantic 2.x.try:
importpydantic.v1aspydantic_v1# If this succeeds we are using Pydantic 2.x, and we import the backwards-compatible# version of BaseModel.exceptImportError:
# In this case, we are using Pydantic 1.x and we import as usual.importpydanticaspydantic_v1
...
frompydantic_v1importBaseModel, ...
...
Describe alternatives you've considered
I tried using a modified version of the above and then specifying an override base class of ..BaseModel_v1:
# We use Pydantic 1.x for backwards compatibility. We import it in a# way that is also forwards compatible with Pydantic 2.x.try:
importpydantic.v1importBaseModelasBaseModel_v1# If this succeeds we are using Pydantic 2.x, but we import the backwards-compatible# version of BaseModel.exceptImportError:
# In this case, we are using Pydantic 1.x and we import as usual.importpydanticimportBaseModelasBaseModel_v1
Any way I try implement the base class override, however, I still end up with an extra (and incorrect) import statement of BaseModel_v1. I tried the base class of BaseModel_v1, .BaseModel_v1, and ..BaseModel_v1.
Ideally, for this workaround to work, an override base class without a . in it would be expected to have already been imported, since its impossible to import a class without a module reference and so rather than import BaseModel_v1 being generated if I set base class of BaseModel_v1, that scenario would just skip an attempt to import.
The closest I could get was this invocation, using the above code in the header file:
Is your feature request related to a problem? Please describe.
I love that this codegen tool exists! We currently rely on models generated with this library, but we need to support compatibility with both Pydantic v1 and v2. I'm struggling with how to do this.
Describe the solution you'd like
I'd like
datamodel-code-generator
to be able to generate "v1" Pydantic models, but in a way that also forwards-compatible with Pydantic 2.This can be implemented with the following:
Describe alternatives you've considered
I tried using a modified version of the above and then specifying an override base class of
..BaseModel_v1
:Any way I try implement the base class override, however, I still end up with an extra (and incorrect) import statement of
BaseModel_v1
. I tried the base class ofBaseModel_v1
,.BaseModel_v1
, and..BaseModel_v1
.Ideally, for this workaround to work, an override base class without a
.
in it would be expected to have already been imported, since its impossible to import a class without a module reference and so rather thanimport BaseModel_v1
being generated if I set base class ofBaseModel_v1
, that scenario would just skip an attempt to import.The closest I could get was this invocation, using the above code in the header file:
Additional context
WIP PR in downstream library here:
The text was updated successfully, but these errors were encountered: