Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add datamodel-code-generator link in pydantic document site (#1532)
* Add datamodel-code-generator link in pydantic document site. * tweak menu * fix blank lines Co-authored-by: Samuel Colvin <samcolvin@gmail.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
- Loading branch information
1 parent
63ec6ff
commit 6e29848
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add datamodel-code-generator link in pydantic document site. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
[datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator/) is a command to generate pydantic models from other data types. | ||
|
||
|
||
* Supported source types | ||
* OpenAPI 3 (YAML/JSON) | ||
* JSON Schema | ||
* JSON/YAML Data (It will be converted to JSON Schema) | ||
|
||
## Install | ||
```bash | ||
pip install datamodel-code-generato | ||
``` | ||
|
||
## Example | ||
In this case, The datamodel-code-generator creates pydantic models from JSON Schema. | ||
```bash | ||
datamodel-codegen --input person.json --input-file-type jsonschema --output model.py | ||
``` | ||
|
||
person.json: | ||
```json | ||
{ | ||
"$id": "person.json", | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "Person", | ||
"type": "object", | ||
"properties": { | ||
"first_name": { | ||
"type": "string", | ||
"description": "The person's first name." | ||
}, | ||
"last_name": { | ||
"type": "string", | ||
"description": "The person's last name." | ||
}, | ||
"age": { | ||
"description": "Age in years.", | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"pets": { | ||
"type": "array", | ||
"items": [ | ||
{ | ||
"$ref": "#/definitions/Pet" | ||
} | ||
] | ||
}, | ||
"comment": { | ||
"type": "null" | ||
} | ||
}, | ||
"required": [ | ||
"first_name", | ||
"last_name" | ||
], | ||
"definitions": { | ||
"Pet": { | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"age": { | ||
"type": "integer" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
model.py: | ||
```py | ||
{!.tmp_examples/generate_models_person_model.py!} | ||
``` | ||
|
||
More information can be found on the | ||
[official documentation](https://koxudaxi.github.io/datamodel-code-generator/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# generated by datamodel-codegen: | ||
# filename: person.json | ||
# timestamp: 2020-05-19T15:07:31+00:00 | ||
from __future__ import annotations | ||
from typing import Any, List, Optional | ||
from pydantic import BaseModel, Field, conint | ||
|
||
|
||
class Pet(BaseModel): | ||
name: Optional[str] = None | ||
age: Optional[int] = None | ||
|
||
|
||
class Person(BaseModel): | ||
first_name: str = Field(..., description="The person's first name.") | ||
last_name: str = Field(..., description="The person's last name.") | ||
age: Optional[conint(ge=0)] = Field(None, description='Age in years.') | ||
pets: Optional[List[Pet]] = None | ||
comment: Optional[Any] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters