datamodel-code-generator
datamodel-code-generator copied to clipboard
Support pydantic.dataclasses as output format
In some projects, I use pydantic.dataclasses to add validation to dataclass models. This is useful on project that requires dataclasses but still want to use pydantic for validation. All fields and type annotation of pydantic are compatible and can be generated in the same way.
We could easily support this as there is only 2 changes with pydantic.BaseModel:
- the class definition :
from pydantic import BaseModel
class MyModel(BaseModel):
id: str
to
from pydantic.dataclasses import dataclass
@dataclass
class MyModel:
id: str
- Ordering of default value fields : With dataclasses, default value fields must be written after fields with no default value. Using BaseModel generation seems to generate fields in definition order without any ordering.
@dataclass
class MyModel:
id: str
description: str = None # This field needs to be after id field
Hi, what would be required to add support for this? I think the nice advantage is that it would add validation to a dataclass and further keep it lightweight. So no additional attributes / methods on the model as Pydantic BaseModel does.